Replace constructors fns with static methods

This commit is contained in:
Erick Tryzelaar 2013-08-15 07:35:44 -07:00
parent 06b41612aa
commit 1ee6b87b2f
3 changed files with 33 additions and 36 deletions

17
hash.rs
View File

@ -59,18 +59,15 @@ pub struct Hasher {
priv len: uint,
}
pub fn Hasher(ht: HashType) -> Hasher {
impl Hasher {
pub fn new(ht: HashType) -> Hasher {
let ctx = unsafe { libcrypto::EVP_MD_CTX_create() };
let (evp, mdlen) = evpmd(ht);
let h = Hasher { evp: evp, ctx: ctx, len: mdlen };
h.init();
h
}
unsafe {
libcrypto::EVP_DigestInit(ctx, evp);
}
impl Hasher {
/// Initializes this hasher
pub fn init(&self) {
unsafe { libcrypto::EVP_DigestInit(self.ctx, self.evp) }
Hasher { evp: evp, ctx: ctx, len: mdlen }
}
/// Update this hasher with more input bytes
@ -110,7 +107,7 @@ impl Drop for Hasher {
* value
*/
pub fn hash(t: HashType, data: &[u8]) -> ~[u8] {
let h = Hasher(t);
let h = Hasher::new(t);
h.update(data);
h.final()
}

30
pkey.rs
View File

@ -74,15 +74,15 @@ pub struct PKey {
priv parts: Parts,
}
pub fn PKey() -> PKey {
PKey {
evp: unsafe { libcrypto::EVP_PKEY_new() },
parts: Neither
}
}
///Represents a public key, optionally with a private key attached.
impl PKey {
pub fn new() -> PKey {
PKey {
evp: unsafe { libcrypto::EVP_PKEY_new() },
parts: Neither,
}
}
fn _tostr(&self, f: extern "C" unsafe fn(*EVP_PKEY, **mut u8) -> c_int) -> ~[u8] {
unsafe {
let len = f(self.evp, ptr::null());
@ -350,8 +350,8 @@ mod tests {
#[test]
fn test_gen_pub() {
let mut k0 = PKey();
let mut k1 = PKey();
let mut k0 = PKey::new();
let mut k1 = PKey::new();
k0.gen(512u);
k1.load_pub(k0.save_pub());
assert!(k0.save_pub() == k1.save_pub());
@ -368,8 +368,8 @@ mod tests {
#[test]
fn test_gen_priv() {
let mut k0 = PKey();
let mut k1 = PKey();
let mut k0 = PKey::new();
let mut k1 = PKey::new();
k0.gen(512u);
k1.load_priv(k0.save_priv());
assert!(k0.save_priv() == k1.save_priv());
@ -386,8 +386,8 @@ mod tests {
#[test]
fn test_encrypt() {
let mut k0 = PKey();
let mut k1 = PKey();
let mut k0 = PKey::new();
let mut k1 = PKey::new();
let msg = ~[0xdeu8, 0xadu8, 0xd0u8, 0x0du8];
k0.gen(512u);
k1.load_pub(k0.save_pub());
@ -398,8 +398,8 @@ mod tests {
#[test]
fn test_sign() {
let mut k0 = PKey();
let mut k1 = PKey();
let mut k0 = PKey::new();
let mut k1 = PKey::new();
let msg = ~[0xdeu8, 0xadu8, 0xd0u8, 0x0du8];
k0.gen(512u);
k1.load_pub(k0.save_pub());

12
symm.rs
View File

@ -61,13 +61,13 @@ pub struct Crypter {
priv blocksize: uint
}
pub fn Crypter(t: Type) -> Crypter {
impl Crypter {
pub fn new(t: Type) -> Crypter {
let ctx = unsafe { libcrypto::EVP_CIPHER_CTX_new() };
let (evp, keylen, blocksz) = evpc(t);
Crypter { evp: evp, ctx: ctx, keylen: keylen, blocksize: blocksz }
}
}
impl Crypter {
/**
* Enables or disables padding. If padding is disabled, total amount of
* data encrypted must be a multiple of block size.
@ -163,7 +163,7 @@ impl Drop for Crypter {
* specified key and iv; returns the resulting (encrypted) data.
*/
pub fn encrypt(t: Type, key: &[u8], iv: ~[u8], data: &[u8]) -> ~[u8] {
let c = Crypter(t);
let c = Crypter::new(t);
c.init(Encrypt, key, iv);
let r = c.update(data);
let rest = c.final();
@ -175,7 +175,7 @@ pub fn encrypt(t: Type, key: &[u8], iv: ~[u8], data: &[u8]) -> ~[u8] {
* specified key and iv; returns the resulting (decrypted) data.
*/
pub fn decrypt(t: Type, key: &[u8], iv: ~[u8], data: &[u8]) -> ~[u8] {
let c = Crypter(t);
let c = Crypter::new(t);
c.init(Decrypt, key, iv);
let r = c.update(data);
let rest = c.final();
@ -201,7 +201,7 @@ mod tests {
let c0 =
~[ 0x8eu8, 0xa2u8, 0xb7u8, 0xcau8, 0x51u8, 0x67u8, 0x45u8, 0xbfu8,
0xeau8, 0xfcu8, 0x49u8, 0x90u8, 0x4bu8, 0x49u8, 0x60u8, 0x89u8 ];
let c = Crypter(AES_256_ECB);
let c = Crypter::new(AES_256_ECB);
c.init(Encrypt, k0, []);
c.pad(false);
let r0 = c.update(p0) + c.final();