#加密解密 #流密码 #chacha20-poly1305 # #加密 #解密 #包装器

chacha20stream

chacha20_poly1305流密码的加密和解密可写包装流

9个稳定版本

2.2.1 2021年8月20日
2.1.0 2021年8月16日
2.0.1 2021年8月14日
1.0.3 2021年5月8日
1.0.2 2021年4月8日

#1601密码学

每月 28 次下载

MIT 许可证

68KB
1.5K SLoC

chacha20_poly1305流包装器

包含一个可写流,该流包装另一个流,在写入之前对输入应用chacha20_poly1305密码,用于加密或解密。

使用示例

使用内存缓冲区加密和解密消息。

// Generate random key and IV for the operations.
let (key, iv) = chacha20stream::keygen();

let input = "Hello world!";

// Encryption into a new `Vec<u8>`. (Any implementor of `io::Write` or `&mut io::Write` can be used.)
let mut sink = Sink::encrypt(Vec::new(), key, iv).expect("Failed to create encryptor");
sink.write_all(input.as_bytes()).unwrap();
sink.flush().unwrap(); // `flush` also clears the in-memory buffer if there is left over data in it.

let output_encrypted = sink.into_inner();

// Decryption into a new `Vec<u8>`. (Any implementor of `std::io::Write` or `&mut std::io::Write` can be used.)
let mut sink = Sink::decrypt(Vec::new(), key, iv).expect("Failed to create decryptor");
sink.write_all(&output_encrypted[..]).unwrap();
sink.flush().unwrap();

let output_decrypted = sink.into_inner();

assert_eq!(&output_decrypted[..], input.as_bytes());

特性

  • smallvec - 如果内存缓冲区足够小,则使用smallveccrate在堆栈上存储内存缓冲区(默认)
  • async - 使用Tokio 0.2 AsyncWrite启用AsyncSink。API与常规Sink相同。
  • explicit_clear - 在操作后显式清除内存缓冲区。
  • ffi - 使用C FFI接口构建(参见include/cc20.h。)输出库生成在target/{debug,release}/libchacha20stream.}{a,so}

许可证

MIT

依赖

~2.2–4MB
~82K SLoC