First commit.
[crypto_lab1.git] / src / crypto.rs
diff --git a/src/crypto.rs b/src/crypto.rs
new file mode 100644 (file)
index 0000000..1e1ddc1
--- /dev/null
@@ -0,0 +1,48 @@
+use std::rand::OsRng;
+use std::vec::Vec;
+use std::io::IoResult;
+use std::rand::Rng;
+
+use openssl::crypto::hash::SHA256;
+use openssl::crypto::hmac::HMAC;
+use std::slice::bytes::copy_memory;
+
+//const KEY_A: &'static [u8] = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d".from_hex().unwrap();
+//static KEY_A: Vec<u8> = vec![1, 2, 3]; // "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d".from_hex().unwrap();
+
+pub struct Crypto {
+   key_a: Vec<u8>,
+   key_c: Vec<u8>
+}
+
+impl Crypto {
+   pub fn new() -> Crypto {
+      Crypto {
+         key_a: [125, 31, 131, 118, 143, 180, 252, 53, 211, 217, 79, 240, 128, 91, 252, 87, 104, 236, 145, 198, 163, 203, 161, 12, 53, 56, 218, 40, 221, 95, 171, 140].to_vec(),
+         key_c: [75, 226, 88, 31, 223, 216, 182, 216, 178, 58, 59, 193, 245, 80, 254, 128, 125, 246, 246, 224, 194, 190, 123, 123, 10, 131, 217, 183, 112, 157, 166, 102].to_vec()
+      }
+   }
+
+   pub fn compute_mac(&self, data: &[u8]) -> [u8, ..10] {
+      let mut hmac = HMAC(SHA256, self.key_a.as_slice());
+      hmac.update(data);
+      let mut result = [0u8, ..10];
+      copy_memory(&mut result, hmac.finalize().slice(0, 9));
+      result
+   }
+}
+
+pub fn encrypt(plaindata: &Vec<u8>) -> Vec<u8> {
+   vec!()
+}
+
+pub fn decrypt(cypherdata: &Vec<u8>) -> Vec<u8> {
+   vec!()
+}
+
+pub fn generate_key(size_byte: uint) -> IoResult<Vec<u8>>  {
+   let mut bytes = Vec::from_elem(size_byte, 0u8);
+   let mut generator = try!(OsRng::new());
+   generator.fill_bytes(bytes.as_mut_slice_());
+   Ok(bytes)
+}
\ No newline at end of file