Use AsRef for backwards compatibility with passing IV as Vec
This commit is contained in:
parent
cbf0cbafbf
commit
d2d20a8377
|
|
@ -1,4 +1,5 @@
|
||||||
use std::iter::repeat;
|
use std::iter::repeat;
|
||||||
|
use std::convert::AsRef;
|
||||||
use libc::{c_int};
|
use libc::{c_int};
|
||||||
|
|
||||||
use ffi;
|
use ffi;
|
||||||
|
|
@ -86,7 +87,7 @@ impl Crypter {
|
||||||
/**
|
/**
|
||||||
* Initializes this crypter.
|
* Initializes this crypter.
|
||||||
*/
|
*/
|
||||||
pub fn init(&self, mode: Mode, key: &[u8], iv: &[u8]) {
|
pub fn init<T: AsRef<[u8]>>(&self, mode: Mode, key: &[u8], iv: T) {
|
||||||
unsafe {
|
unsafe {
|
||||||
let mode = match mode {
|
let mode = match mode {
|
||||||
Mode::Encrypt => 1 as c_int,
|
Mode::Encrypt => 1 as c_int,
|
||||||
|
|
@ -98,7 +99,7 @@ impl Crypter {
|
||||||
self.ctx,
|
self.ctx,
|
||||||
self.evp,
|
self.evp,
|
||||||
key.as_ptr(),
|
key.as_ptr(),
|
||||||
iv.as_ptr(),
|
iv.as_ref().as_ptr(),
|
||||||
mode
|
mode
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -157,7 +158,7 @@ impl Drop for Crypter {
|
||||||
* Encrypts data, using the specified crypter type in encrypt mode with the
|
* Encrypts data, using the specified crypter type in encrypt mode with the
|
||||||
* specified key and iv; returns the resulting (encrypted) data.
|
* specified key and iv; returns the resulting (encrypted) data.
|
||||||
*/
|
*/
|
||||||
pub fn encrypt(t: Type, key: &[u8], iv: &[u8], data: &[u8]) -> Vec<u8> {
|
pub fn encrypt<T: AsRef<[u8]>>(t: Type, key: &[u8], iv: T, data: &[u8]) -> Vec<u8> {
|
||||||
let c = Crypter::new(t);
|
let c = Crypter::new(t);
|
||||||
c.init(Mode::Encrypt, key, iv);
|
c.init(Mode::Encrypt, key, iv);
|
||||||
let mut r = c.update(data);
|
let mut r = c.update(data);
|
||||||
|
|
@ -170,7 +171,7 @@ pub fn encrypt(t: Type, key: &[u8], iv: &[u8], data: &[u8]) -> Vec<u8> {
|
||||||
* Decrypts data, using the specified crypter type in decrypt mode with the
|
* Decrypts data, using the specified crypter type in decrypt mode with the
|
||||||
* specified key and iv; returns the resulting (decrypted) data.
|
* specified key and iv; returns the resulting (decrypted) data.
|
||||||
*/
|
*/
|
||||||
pub fn decrypt(t: Type, key: &[u8], iv: &[u8], data: &[u8]) -> Vec<u8> {
|
pub fn decrypt<T: AsRef<[u8]>>(t: Type, key: &[u8], iv: T, data: &[u8]) -> Vec<u8> {
|
||||||
let c = Crypter::new(t);
|
let c = Crypter::new(t);
|
||||||
c.init(Mode::Decrypt, key, iv);
|
c.init(Mode::Decrypt, key, iv);
|
||||||
let mut r = c.update(data);
|
let mut r = c.update(data);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue