about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-09-25 14:25:52 -0700
committerbors <bors@rust-lang.org>2013-09-25 14:25:52 -0700
commit24d46a0f45b4a0d6198b9d23dad2f7faa5a05d92 (patch)
treec5a9ba141d2f3656aae4c40979d3a419a2d590e6 /src
parentaf25f58ac3da45899ed65b3af965150c8a90dcda (diff)
parentb82e0d32ce6bca2f2fb64ce078d400a1aab0d7c9 (diff)
downloadrust-24d46a0f45b4a0d6198b9d23dad2f7faa5a05d92.tar.gz
rust-24d46a0f45b4a0d6198b9d23dad2f7faa5a05d92.zip
auto merge of #9345 : Dretch/rust/digest-result-bytes, r=cmr
I would find this function useful.
Diffstat (limited to 'src')
-rw-r--r--src/libextra/crypto/cryptoutil.rs5
-rw-r--r--src/libextra/crypto/digest.rs25
2 files changed, 16 insertions, 14 deletions
diff --git a/src/libextra/crypto/cryptoutil.rs b/src/libextra/crypto/cryptoutil.rs
index 4eba3e13eea..3573bb55948 100644
--- a/src/libextra/crypto/cryptoutil.rs
+++ b/src/libextra/crypto/cryptoutil.rs
@@ -352,6 +352,7 @@ mod test {
 
     use cryptoutil::{add_bytes_to_bits, add_bytes_to_bits_tuple};
     use digest::Digest;
+    use hex::FromHex;
 
     /// Feed 1,000,000 'a's into the digest with varying input sizes and check that the result is
     /// correct.
@@ -372,8 +373,10 @@ mod test {
         }
 
         let result_str = digest.result_str();
+        let result_bytes = digest.result_bytes();
 
-        assert!(expected == result_str);
+        assert_eq!(expected, result_str.as_slice());
+        assert_eq!(expected.from_hex().unwrap(), result_bytes);
     }
 
     // A normal addition - no overflow occurs
diff --git a/src/libextra/crypto/digest.rs b/src/libextra/crypto/digest.rs
index d2d6b540cff..85c256c47a3 100644
--- a/src/libextra/crypto/digest.rs
+++ b/src/libextra/crypto/digest.rs
@@ -10,6 +10,8 @@
 
 use std::vec;
 
+use hex::ToHex;
+
 
 /**
  * The Digest trait specifies an interface common to digest functions, such as SHA-1 and the SHA-2
@@ -58,23 +60,20 @@ pub trait Digest {
 
     /**
      * Convenience function that retrieves the result of a digest as a
-     * ~str in hexadecimal format.
+     * newly allocated vec of bytes.
      */
-    fn result_str(&mut self) -> ~str {
+    fn result_bytes(&mut self) -> ~[u8] {
         let mut buf = vec::from_elem((self.output_bits()+7)/8, 0u8);
         self.result(buf);
-        return to_hex(buf);
+        buf
     }
-}
 
-fn to_hex(rr: &[u8]) -> ~str {
-    let mut s = ~"";
-    for b in rr.iter() {
-        let hex = (*b as uint).to_str_radix(16u);
-        if hex.len() == 1 {
-            s.push_char('0');
-        }
-        s.push_str(hex);
+    /**
+     * Convenience function that retrieves the result of a digest as a
+     * ~str in hexadecimal format.
+     */
+    fn result_str(&mut self) -> ~str {
+        self.result_bytes().to_hex()
     }
-    return s;
 }
+