aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorErick Tryzelaar <[email protected]>2013-08-15 07:35:44 -0700
committerErick Tryzelaar <[email protected]>2013-08-15 07:55:23 -0700
commit1ee6b87b2f71112bbf9ec05e2ea6c36f1592321a (patch)
tree8fab2cbc6e2d5f74e9948313a54eaefcdc1b98ff
parentRename crypto.rc to crypto.rs (diff)
downloadrust-openssl-1ee6b87b2f71112bbf9ec05e2ea6c36f1592321a.tar.xz
rust-openssl-1ee6b87b2f71112bbf9ec05e2ea6c36f1592321a.zip
Replace constructors fns with static methods
-rw-r--r--hash.rs21
-rw-r--r--pkey.rs30
-rw-r--r--symm.rs18
3 files changed, 33 insertions, 36 deletions
diff --git a/hash.rs b/hash.rs
index 421cdc23..8cae213d 100644
--- a/hash.rs
+++ b/hash.rs
@@ -59,18 +59,15 @@ pub struct Hasher {
priv len: uint,
}
-pub fn Hasher(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
-}
-
impl Hasher {
- /// Initializes this hasher
- pub fn init(&self) {
- unsafe { libcrypto::EVP_DigestInit(self.ctx, self.evp) }
+ pub fn new(ht: HashType) -> Hasher {
+ let ctx = unsafe { libcrypto::EVP_MD_CTX_create() };
+ let (evp, mdlen) = evpmd(ht);
+ unsafe {
+ libcrypto::EVP_DigestInit(ctx, 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()
}
diff --git a/pkey.rs b/pkey.rs
index 2da84458..dcadfcaa 100644
--- a/pkey.rs
+++ b/pkey.rs
@@ -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());
diff --git a/symm.rs b/symm.rs
index a675d094..c4ed97e9 100644
--- a/symm.rs
+++ b/symm.rs
@@ -61,13 +61,13 @@ pub struct Crypter {
priv blocksize: uint
}
-pub fn Crypter(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 {
+ 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 }
+ }
+
/**
* 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();