Skip to content

Allowed the creation of key for AES-GCM #27

@WingZer0o

Description

@WingZer0o

For reference

let hex_string = "8000000000000000000000000000000000000000000000000000000000000001";
    // Convert hexadecimal string to Vec<u8>
    let bytes: Vec<u8> = Vec::from_hex(hex_string).expect("Invalid hexadecimal string");
    let key: &Key<Aes256Gcm> = GenericArray::from_slice(&bytes);
    
    // Transformed from a byte array:
    // let key: &[u8; 32] = &[42; 32];
    // let key: &Key<Aes256Gcm> = key.into();
    
    // // Note that you can get byte array from slice using the `TryInto` trait:
    // let key: &[u8] = &[42; 32];
    // let key: [u8; 32] = key.try_into().unwrap();
    // let key = Key::<Aes256Gcm>::from_slice(&key);
    
    // Alternatively, the key can be transformed directly from a byte slice
    // (panicks on length mismatch):
    // let key = Aes256Gcm::generate_key(OsRng);
    let cipher = Aes256Gcm::new(&key);
    let nonce = Aes256Gcm::generate_nonce(&mut OsRng);
    let ciphertext = cipher.encrypt(&nonce, b"plaintext message".as_ref()).unwrap();
    let plaintext = cipher.decrypt(&nonce, ciphertext.as_ref()).unwrap();
    assert_eq!(&plaintext, b"plaintext message");
    // The encryption key can be generated randomly:
    // let key = KeyInit::new_from_slice(hex_key).unwrap();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions