Created
October 6, 2023 09:55
-
-
Save ThrowsException/6df4a1bf1c752b6eb196d8215afdecb5 to your computer and use it in GitHub Desktop.
Rust Bcrypt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use bcrypt::{DEFAULT_COST, hash_with_salt}; | |
use hex::encode; | |
use sha2::{Digest, Sha512}; | |
fn main() { | |
println!("Hello, world!"); | |
let my_string = "LSlIhPLWw1j1UjZFG/bJde"; | |
// let mut my: [u8; 16] = my_string[0..16].try_into().unwrap() | |
let my: [u8; 16] = my_string[0..16].as_bytes().try_into().unwrap(); | |
println!("{:?}", my); | |
let s = &my_string[0..16]; | |
println!("{:?}", s); | |
println!("{:?}", encode(my)); | |
// let mut a: [u8; 16] = Default::default(); | |
// a.copy_from_slice(my_string[0..16]); | |
let pass = "helloworld"; | |
// Create a new SHA-512 hasher | |
let mut hasher = Sha512::new(); | |
// Update the hasher with the bytes of the input string | |
hasher.update(pass.as_bytes()); | |
// Finalize the hash and store it in a fixed-size array | |
let result = hasher.finalize(); | |
// Convert the hash result to a hexadecimal string | |
let hash_hex = result.iter().map(|byte| format!("{:02x}", byte)).collect::<String>(); | |
println!("SHA-512 hash: {}", hash_hex); | |
// let byte_slice: [u8; 16] = &my_string.as_bytes()[0..16]; | |
// let hashed = hash_with_salt(hash_hex, DEFAULT_COST, my); | |
// println!("{:?}", hashed); | |
let res = bcrypt::bcrypt(12, my, &result); | |
println!("{:?}", encode(res)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment