mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-12 06:53:05 +00:00
Crypto: Remove DigestUtil and convert to default methods on the Digest trait.
This commit is contained in:
parent
ee3f75366c
commit
2cbe312343
@ -8,10 +8,10 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
|
||||
use std::uint;
|
||||
use std::vec;
|
||||
|
||||
|
||||
/**
|
||||
* The Digest trait specifies an interface common to digest functions, such as SHA-1 and the SHA-2
|
||||
* family of digest functions.
|
||||
@ -28,6 +28,10 @@ pub trait Digest {
|
||||
|
||||
/**
|
||||
* Retrieve the digest result. This method may be called multiple times.
|
||||
*
|
||||
* # Arguments
|
||||
*
|
||||
* * out - the vector to hold the result. Must be large enough to contain output_bits().
|
||||
*/
|
||||
fn result(&mut self, out: &mut [u8]);
|
||||
|
||||
@ -41,6 +45,27 @@ pub trait Digest {
|
||||
* Get the output size in bits.
|
||||
*/
|
||||
fn output_bits(&self) -> uint;
|
||||
|
||||
/**
|
||||
* Convenience functon that feeds a string into a digest
|
||||
*
|
||||
* # Arguments
|
||||
*
|
||||
* * in The string to feed into the digest
|
||||
*/
|
||||
fn input_str(&mut self, input: &str) {
|
||||
self.input(input.as_bytes());
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience functon that retrieves the result of a digest as a
|
||||
* ~str in hexadecimal format.
|
||||
*/
|
||||
fn result_str(&mut self) -> ~str {
|
||||
let mut buf = vec::from_elem((self.output_bits()+7)/8, 0u8);
|
||||
self.result(buf);
|
||||
return to_hex(buf);
|
||||
}
|
||||
}
|
||||
|
||||
fn to_hex(rr: &[u8]) -> ~str {
|
||||
@ -54,34 +79,3 @@ fn to_hex(rr: &[u8]) -> ~str {
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
/// Contains utility methods for Digests.
|
||||
/// FIXME: #7339: Convert to default methods when issues with them are resolved.
|
||||
pub trait DigestUtil {
|
||||
/**
|
||||
* Convenience functon that feeds a string into a digest
|
||||
*
|
||||
* # Arguments
|
||||
*
|
||||
* * in The string to feed into the digest
|
||||
*/
|
||||
fn input_str(&mut self, input: &str);
|
||||
|
||||
/**
|
||||
* Convenience functon that retrieves the result of a digest as a
|
||||
* ~str in hexadecimal format.
|
||||
*/
|
||||
fn result_str(&mut self) -> ~str;
|
||||
}
|
||||
|
||||
impl<D: Digest> DigestUtil for D {
|
||||
fn input_str(&mut self, input: &str) {
|
||||
self.input(input.as_bytes());
|
||||
}
|
||||
|
||||
fn result_str(&mut self) -> ~str {
|
||||
let mut buf = vec::from_elem((self.output_bits()+7)/8, 0u8);
|
||||
self.result(buf);
|
||||
return to_hex(buf);
|
||||
}
|
||||
}
|
||||
|
@ -241,7 +241,7 @@ impl Digest for Sha1 {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
use digest::{Digest, DigestUtil};
|
||||
use digest::Digest;
|
||||
use sha1::Sha1;
|
||||
|
||||
#[deriving(Clone)]
|
||||
|
@ -756,7 +756,7 @@ static H224: [u32, ..8] = [
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use digest::{Digest, DigestUtil};
|
||||
use digest::Digest;
|
||||
use sha2::{Sha512, Sha384, Sha512Trunc256, Sha512Trunc224, Sha256, Sha224};
|
||||
|
||||
struct Test {
|
||||
|
@ -10,8 +10,7 @@
|
||||
|
||||
#[allow(missing_doc)];
|
||||
|
||||
|
||||
use digest::DigestUtil;
|
||||
use digest::Digest;
|
||||
use json;
|
||||
use sha1::Sha1;
|
||||
use serialize::{Encoder, Encodable, Decoder, Decodable};
|
||||
|
Loading…
Reference in New Issue
Block a user