Skip to content

Commit

Permalink
Blueokanna Update
Browse files Browse the repository at this point in the history
  • Loading branch information
blueokanna committed Nov 21, 2024
1 parent 095f70b commit 5de53b7
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 41 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "BlueHash"
name = "bluehash"
description = "A highly secure alternative to existing custom digest algorithms."
authors = ["[email protected]"]
version = "0.1.6"
version = "0.1.7"
edition = "2021"
license = "Apache-2.0"
readme = "README.md"
Expand Down
14 changes: 8 additions & 6 deletions README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,26 @@
## 使用示例

```rust
use std::fmt::Write;
use bluehash::{BlueHashCore, Digest, DigestSize};
fn main() {
// 测试数据
let test_data = b"Hello, world! This is a test message for BlueHash";

// 初始化 BlueHash128 哈希器
let mut hasher128 = BlueHash128::new(DigestSize::Bit128);
let mut hasher128 = BlueHashCore::new(DigestSize::Bit128);
hasher128.update(test_data);
let result128 = hasher128.finalize();
println!("BlueHash128 Result: {}", to_hex_string(&result128));

// 初始化 BlueHash256 哈希器
let mut hasher256 = BlueHash256::new(DigestSize::Bit256);
let mut hasher256 = BlueHashCore::new(DigestSize::Bit256);
hasher256.update(test_data);
let result256 = hasher256.finalize();
println!("BlueHash256 Result: {}", to_hex_string(&result256));

// 初始化 BlueHash512 哈希器
let mut hasher512 = BlueHash512::new(DigestSize::Bit512);
let mut hasher512 = BlueHashCore::new(DigestSize::Bit512);
hasher512.update(test_data);
let result512 = hasher512.finalize();
println!("BlueHash512 Result: {}", to_hex_string(&result512));
Expand All @@ -43,9 +45,9 @@ fn to_hex_string(bytes: &[u8]) -> String {
```
### 你会得到输出结果如下:
```
The full 128-bit hash result is: e68e6528271d5623a8e195bb6ac7cff3
The full 256-bit hash result is: c472cbe52b0f1b44f3aa1cec8d56dc578eb75048be19ca5edc6d349c2b5c7ceb
The full 512-bit hash result is: 46ae2678b8ad6bf066313512f26ceba12211c6087b9f6d7b6223dbcc18687440699b65b333db95b978aba1440c27b5ad5833bbd796380f66028ffa6a9a44482e
BlueHash128 Result: 5e2bf33d152fd49dcd27d943ee99dc5f
BlueHash256 Result: d39c54a9c538b1372420c11b01a5c7b224eefd02a3390b7bcb86bf1a99d4b73e
BlueHash512 Result: ba0951d578a9c92c6296d620b0a5a8a7b2557d8d28201b14b61603e850e94b71fe6e7922b9aa4d0eb4c184c0c3c26196b79a67005ee04539f79c14b1fb1d47e5
```

## 关键组件
Expand Down
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,27 @@ This documentation provides an explanation of the algorithm's core components, i
## Use Example

```rust
use std::fmt::Write;
use bluehash::{BlueHashCore, Digest, DigestSize};

fn main() {
// Test Data
let test_data = b"Hello, world! This is a test message for BlueHash";

// Initialize the BlueHash128 hasher
let mut hasher128 = BlueHash128::new(DigestSize::Bit128);
let mut hasher128 = BlueHashCore::new(DigestSize::Bit128);
hasher128.update(test_data);
let result128 = hasher128.finalize();
println!("BlueHash128 Result: {}", to_hex_string(&result128));

// Initialize the BlueHash256 hasher
let mut hasher256 = BlueHash256::new(DigestSize::Bit256);
let mut hasher256 = BlueHashCore::new(DigestSize::Bit256);
hasher256.update(test_data);
let result256 = hasher256.finalize();
println!("BlueHash256 Result: {}", to_hex_string(&result256));

// Initialize the BlueHash512 hasher
let mut hasher512 = BlueHash512::new(DigestSize::Bit512);
let mut hasher512 = BlueHashCore::new(DigestSize::Bit512);
hasher512.update(test_data);
let result512 = hasher512.finalize();
println!("BlueHash512 Result: {}", to_hex_string(&result512));
Expand All @@ -43,9 +46,9 @@ fn to_hex_string(bytes: &[u8]) -> String {
```
### You will get output for example code
```
The full 128-bit hash result is: e68e6528271d5623a8e195bb6ac7cff3
The full 256-bit hash result is: c472cbe52b0f1b44f3aa1cec8d56dc578eb75048be19ca5edc6d349c2b5c7ceb
The full 512-bit hash result is: 46ae2678b8ad6bf066313512f26ceba12211c6087b9f6d7b6223dbcc18687440699b65b333db95b978aba1440c27b5ad5833bbd796380f66028ffa6a9a44482e
BlueHash128 Result: 5e2bf33d152fd49dcd27d943ee99dc5f
BlueHash256 Result: d39c54a9c538b1372420c11b01a5c7b224eefd02a3390b7bcb86bf1a99d4b73e
BlueHash512 Result: ba0951d578a9c92c6296d620b0a5a8a7b2557d8d28201b14b61603e850e94b71fe6e7922b9aa4d0eb4c184c0c3c26196b79a67005ee04539f79c14b1fb1d47e5
```

## Key Components
Expand Down
28 changes: 14 additions & 14 deletions benches/bluebench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::time::Duration;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use rand::Rng;
use rayon::prelude::*;
use BlueHash::{Digest, DigestSize};
use bluehash::{Digest, DigestSize,BlueHashCore};

// 并行碰撞测试
pub fn parallel_collision_test(digest_size: DigestSize, trials: usize, num_threads: usize) -> f64 {
Expand All @@ -26,7 +26,7 @@ pub fn parallel_collision_test(digest_size: DigestSize, trials: usize, num_threa

for _ in 0..trials_per_thread {
let data: Vec<u8> = (0..32).map(|_| rng.gen()).collect();
let mut hash = BlueHash::BlueHashCore::new(digest_size);
let mut hash = BlueHashCore::new(digest_size);
hash.update(&data);
let result = hash.finalize();

Expand Down Expand Up @@ -57,7 +57,7 @@ pub fn differential_attack_test(digest_size: DigestSize, trials: usize) -> f64 {
let avalanche_effects: Vec<f64> = (0..trials).into_par_iter().map(|_| {
let mut rng = rand::thread_rng();
let data: Vec<u8> = (0..32).map(|_| rng.gen()).collect();
let mut hash = BlueHash::BlueHashCore::new(digest_size);
let mut hash = BlueHashCore::new(digest_size);
hash.update(&data);
let original_hash = hash.finalize();

Expand All @@ -69,7 +69,7 @@ pub fn differential_attack_test(digest_size: DigestSize, trials: usize) -> f64 {
_ => {}
}

let mut modified_hash = BlueHash::BlueHashCore::new(digest_size);
let mut modified_hash = BlueHashCore::new(digest_size);
modified_hash.update(&modified_data);
let modified_result = modified_hash.finalize();

Expand All @@ -87,14 +87,14 @@ pub fn second_preimage_attack(digest_size: DigestSize, trials: usize) -> f64 {
(0..trials).into_par_iter().map(|_| {
let mut rng = rand::thread_rng();
let data: Vec<u8> = (0..32).map(|_| rng.gen()).collect();
let mut hash = BlueHash::BlueHashCore::new(digest_size);
let mut hash = BlueHashCore::new(digest_size);
hash.update(&data);
let original_hash = hash.finalize();

let mut second_data = data.clone();
second_data[0] ^= 0x01;

let mut second_hash = BlueHash::BlueHashCore::new(digest_size);
let mut second_hash = BlueHashCore::new(digest_size);
second_hash.update(&second_data);
let second_hash_result = second_hash.finalize();

Expand All @@ -107,12 +107,12 @@ pub fn forward_security_test(digest_size: DigestSize, trials: usize) -> f64 {
(0..trials).into_par_iter().map(|_| {
let mut rng = rand::thread_rng();
let data: Vec<u8> = (0..32).map(|_| rng.gen()).collect();
let mut hash = BlueHash::BlueHashCore::new(digest_size);
let mut hash = BlueHashCore::new(digest_size);
hash.update(&data);
let hash_result = hash.finalize();

let guess: Vec<u8> = (0..32).map(|_| rng.gen()).collect();
let mut guess_hash = BlueHash::BlueHashCore::new(digest_size);
let mut guess_hash = BlueHashCore::new(digest_size);
guess_hash.update(&guess);
let guess_result = guess_hash.finalize();

Expand All @@ -125,7 +125,7 @@ pub fn birthday_attack(digest_size: DigestSize, trials: usize) -> f64 {
(0..trials).into_par_iter().map(|_| {
let mut rng = rand::thread_rng();
let data: Vec<u8> = (0..32).map(|_| rng.gen()).collect();
let mut hash = BlueHash::BlueHashCore::new(digest_size);
let mut hash = BlueHashCore::new(digest_size);
hash.update(&data);
let result = hash.finalize();

Expand All @@ -139,15 +139,15 @@ pub fn length_extension_attack(digest_size: DigestSize, trials: usize) -> f64 {
(0..trials).into_par_iter().map(|_| {
let mut rng = rand::thread_rng();
let data: Vec<u8> = (0..32).map(|_| rng.gen()).collect();
let mut hash = BlueHash::BlueHashCore::new(digest_size);
let mut hash = BlueHashCore::new(digest_size);
hash.update(&data);
let original_hash = hash.finalize();

let extra_data: Vec<u8> = (0..16).map(|_| rng.gen()).collect();
let mut attack_data = data.clone();
attack_data.extend(extra_data.clone());

let mut attack_hash = BlueHash::BlueHashCore::new(digest_size);
let mut attack_hash = BlueHashCore::new(digest_size);
attack_hash.update(&attack_data);
let attack_result = attack_hash.finalize();

Expand All @@ -161,23 +161,23 @@ pub fn bench_bluehash(c: &mut Criterion) {
// 测试 BlueHash 不同摘要大小的性能
c.bench_function("BlueHash 128-bit", |b| {
b.iter(|| {
let mut hash = BlueHash::BlueHashCore::new(DigestSize::Bit128);
let mut hash = BlueHashCore::new(DigestSize::Bit128);
hash.update(black_box(data));
black_box(hash.finalize());
});
});

c.bench_function("BlueHash 256-bit", |b| {
b.iter(|| {
let mut hash = BlueHash::BlueHashCore::new(DigestSize::Bit256);
let mut hash = BlueHashCore::new(DigestSize::Bit256);
hash.update(black_box(data));
black_box(hash.finalize());
});
});

c.bench_function("BlueHash 512-bit", |b| {
b.iter(|| {
let mut hash = BlueHash::BlueHashCore::new(DigestSize::Bit512);
let mut hash = BlueHashCore::new(DigestSize::Bit512);
hash.update(black_box(data));
black_box(hash.finalize());
});
Expand Down
24 changes: 12 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
//! BlueHash: A cryptographic hash function with quantum-resistant features.
//!
//! This library provides the implementation of the BlueHash algorithm, designed to
//! resist quantum attacks while maintaining high security. It includes state manipulation,
//! constant generation, and noise-based perturbations inspired by lattice-based cryptography.
//! This library implements the BlueHash algorithm, designed to resist quantum attacks
//! while maintaining high security. It includes state manipulation, constant generation,
//! and noise-based perturbations inspired by lattice-based cryptography.
//!
//! Full details and source code: https://github.com/blueokanna/BlueHash.
//!
//! # BlueHash Usage
//! # BlueHash Usage Example (BlueHash128)
//!
//! ```rust
//! use BlueHash::{BlueHash128, BlueHash256, BlueHash512, DigestSize};
//! use std::fmt::Write;
//!
//! use bluehash::BlueHashCore;
//! use bluehash::DigestSize;
//! use bluehash::Digest;
//! fn main() {
//! use BlueHash::Digest;
//! let test_data = b"Hello, world! This is a test message for BlueHash";
//!
//! let mut hasher128 = BlueHash128::new(DigestSize::Bit128);
//! let mut hasher128 = BlueHashCore::new(DigestSize::Bit128);
//! hasher128.update(test_data);
//! let result128 = hasher128.finalize();
//! println!("BlueHash128 Result: {}", to_hex_string(&result128));
//! }
//!
//! // Helper function to convert bytes to a hexadecimal string
//! fn to_hex_string(bytes: &[u8]) -> String {
//! let mut hex = String::new();
//! for byte in bytes {
Expand All @@ -30,8 +30,8 @@
//! }
//! ```
//!
//! You may also see [BlueHash][1] readme.
//! You may also refer to the [BlueHash][1] readme for more information.
//!
pub mod constants;
pub mod noise;
Expand Down
2 changes: 1 addition & 1 deletion src/noise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn generate_lwe_noise(input_data: &[u8], round: usize, prime: u64) -> u64 {

// Iterate over input_data, applying secure operations to generate noise
for (i, &byte) in input_data.iter().enumerate() {
let multiplied = (byte as u64).wrapping_mul((i.wrapping_add(1)) as u64); // Avoid zero multiplication
let multiplied = (byte as u64).wrapping_mul(i.wrapping_add(1) as u64); // Avoid zero multiplication
noise = noise.wrapping_add(multiplied);
noise = noise.rotate_left(7); // Distribute noise with rotations
}
Expand Down

0 comments on commit 5de53b7

Please sign in to comment.