Skip to content

Commit 832fe32

Browse files
committed
auto merge of #7207 : DaGenix/rust/sha2, r=pcwalton
This pull request contains an unoptimized implementation of most of the SHA-2 functions (everything but the variable output size versions). I also created a common trait (Digest) for all of the interesting methods for working with digests and updated the existing SHA-1 code to use it. Finally, while working with the SHA-1 code, I got rid of the use of @ types and type objects. I've tested all functions against the Wikipedia test vectors. Additionally, I tested the SHA-512, 384, and 256 variants against the Java implementations of those digests. I did my best to try to follow Rust conventions, but, there are so many different conventions in the code base right now that I'm not sure if I'm following the correct one or not. Anyway, I'm happy to rework if I didn't get the coding convention right (or if there are bugs!).
2 parents ac4211e + 711273f commit 832fe32

File tree

7 files changed

+1600
-418
lines changed

7 files changed

+1600
-418
lines changed

RELEASES.txt

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Version 0.7 (July 2013)
7878
* extra: `BigInt`, `BigUint` implement numeric and comparison traits.
7979
* extra: `term` uses terminfo now, is more correct.
8080
* extra: `arc` functions converted to methods.
81+
* extra: Implementation of fixed output size variations of SHA-2.
8182

8283
* Tooling
8384
* `unused_unsafe` lint mode for detecting unnecessary `unsafe` blocks.

src/libextra/crypto/digest.rs

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use core::prelude::*;
12+
13+
use core::uint;
14+
use core::vec;
15+
16+
/**
17+
* The Digest trait specifies an interface common to digest functions, such as SHA-1 and the SHA-2
18+
* family of digest functions.
19+
*/
20+
pub trait Digest {
21+
/**
22+
* Provide message data.
23+
*
24+
* # Arguments
25+
*
26+
* * input - A vector of message data
27+
*/
28+
fn input(&mut self, input: &[u8]);
29+
30+
/**
31+
* Retrieve the digest result. This method may be called multiple times.
32+
*/
33+
fn result(&mut self, out: &mut [u8]);
34+
35+
/**
36+
* Reset the digest. This method must be called after result() and before supplying more
37+
* data.
38+
*/
39+
fn reset(&mut self);
40+
41+
/**
42+
* Get the output size in bits.
43+
*/
44+
fn output_bits(&self) -> uint;
45+
}
46+
47+
fn to_hex(rr: &[u8]) -> ~str {
48+
let mut s = ~"";
49+
for rr.iter().advance() |b| {
50+
let hex = uint::to_str_radix(*b as uint, 16u);
51+
if hex.len() == 1 {
52+
s += "0";
53+
}
54+
s += hex;
55+
}
56+
return s;
57+
}
58+
59+
/// Contains utility methods for Digests.
60+
/// FIXME: #7339: Convert to default methods when issues with them are resolved.
61+
pub trait DigestUtil {
62+
/**
63+
* Convenience functon that feeds a string into a digest
64+
*
65+
* # Arguments
66+
*
67+
* * in The string to feed into the digest
68+
*/
69+
fn input_str(&mut self, in: &str);
70+
71+
/**
72+
* Convenience functon that retrieves the result of a digest as a
73+
* ~str in hexadecimal format.
74+
*/
75+
fn result_str(&mut self) -> ~str;
76+
}
77+
78+
impl<D: Digest> DigestUtil for D {
79+
fn input_str(&mut self, in: &str) {
80+
self.input(in.as_bytes());
81+
}
82+
83+
fn result_str(&mut self) -> ~str {
84+
let mut buf = vec::from_elem((self.output_bits()+7)/8, 0u8);
85+
self.result(buf);
86+
return to_hex(buf);
87+
}
88+
}

0 commit comments

Comments
 (0)