Module ubiq::encryption

source ·
Expand description

Interfaces for encrypting data

Examples

Simple

use ubiq::credentials::Credentials;
use ubiq::encryption::encrypt;
use ubiq::decryption::decrypt;

let creds = Credentials::new(None, None).unwrap();

let ct = encrypt(&creds, b"abc").unwrap();
let pt = decrypt(&creds, &ct).unwrap();

assert!(pt != ct);
assert!(pt == b"abc");

Piecewise

use ubiq::credentials::Credentials;
use ubiq::encryption::Encryption;
use ubiq::decryption::decrypt;

let creds = Credentials::new(None, None).unwrap();
let pt = b"abcdefghijklmnopqrstuvwxyz";

// note that we pass `1` to the new() function, indicating
// that the encryption key will be used once
let mut enc = Encryption::new(&creds, 1).unwrap();

/*
 * pt can be passed to the encryption process in
 * as many or as few pieces as desired
 */

let mut ct = enc.begin().unwrap();
ct.extend(enc.update(&pt[0..4]).unwrap());
ct.extend(enc.update(&pt[4..11]).unwrap());
ct.extend(enc.update(&pt[11..]).unwrap());
ct.extend(enc.end().unwrap());

let rec = decrypt(&creds, &ct).unwrap();

assert!(pt != &ct[..]);
assert!(pt == &rec[..]);

/*
 * if the encryption object was created for more than
 * a single use (by passing a number larger than 1 to
 * the new() function), then the enc object could now
 * be reused by following the begin(), update()...,
 * end() process shown above for as many times as
 * specified by the call to new()
 */

Structs

  • Structure encompassing parameters used for encrypting data

Functions

  • Encrypt a single plaintext with a unique key