Struct sha::sha1::Sha1 [-] [+] [src]

pub struct Sha1(pub [u32; 5], _);

Sha1 structure interface to the SHA-1 message digest algorithm.

Examples

The following sections are some typical examples illustrating the use of the Sha1 structure.

Example digesting the empty message with SHA-1

use std::default::Default;
use self::sha::sha1::Sha1;
use self::sha::utils::{Digest, DigestExt};

assert_eq!(Sha1::default().digest("".as_bytes()).to_hex().as_slice(),
           "da39a3ee5e6b4b0d3255bfef95601890afd80709");

Example digesting the message "abc" with SHA-1.

use std::default::Default;
use self::sha::sha1::Sha1;
use self::sha::utils::{Digest, DigestExt};

assert_eq!(Sha1::default().digest("abc".as_bytes()).to_hex().as_slice(),
           "a9993e364706816aba3e25717850c26c9cd0d89d");

Example digesting the message with one million repetitions of "a" with SHA-1.

use std::default::Default;
use self::sha::utils::ReadPad;
use self::sha::sha1;

{
    let mut state: [u32; 5] = sha1::consts::H;
    let block: [u8; 64] = [0x61; 64]; // "a"
     
    // 15625 blocks of 64 bytes each is 1 million bytes.
    for _ in 0 .. 15625 - 1 {
        sha1::ops::digest_block(&mut state, &block[..]);
    }

    // incremental digest requires that you do the padding
    let mut pad_blocks: Vec<u8> = block.to_vec();
    sha1::ops::pad(1000000).read_pad(&mut pad_blocks);
    for pad_block in (&pad_blocks[..]).chunks(64) {
        sha1::ops::digest_block(&mut state, &pad_block[..]);
    }

    // serialize the digest state
    let hash: String = format!("{:08x}{:08x}{:08x}{:08x}{:08x}", 
                               state[0], state[1], state[2], 
                               state[3], state[4]);

    assert_eq!(hash.as_slice(),
               "34aa973cd4c4daa4f61eeb2bdbad27316534016f");
}

Trait Implementations

impl Default for Sha1

fn default() -> Sha1

Construct a default Sha1 object.

impl Reset for Sha1

fn reset(&mut self)

(Step 0) Reset the state

impl Write for Sha1

fn write(&mut self, buf: &[u8]) -> Result<usize>

(Step 1) Write to buffer

fn flush(&mut self) -> Result<()>

Digest buffer

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error>

impl Read for Sha1

fn read(&mut self, buf: &mut [u8]) -> Result<usize>

(Step 4) Read state as big-endian

The buffer length must be a multiple of 4.

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<(), Error>

fn read_to_string(&mut self, buf: &mut String) -> Result<(), Error>

impl Hasher for Sha1

fn finish(&self) -> u64

Get the first 8 bytes of the state

fn write(&mut self, buf: &[u8])

Write to buffer

fn write_u8(&mut self, i: u8)

fn write_u16(&mut self, i: u16)

fn write_u32(&mut self, i: u32)

fn write_u64(&mut self, i: u64)

fn write_usize(&mut self, i: usize)

fn write_i8(&mut self, i: i8)

fn write_i16(&mut self, i: i16)

fn write_i32(&mut self, i: i32)

fn write_i64(&mut self, i: i64)

fn write_isize(&mut self, i: isize)

impl Digest for Sha1

fn digest(&mut self, msg: &[u8]) -> &mut Self

fn to_bytes_len(&mut self, len: usize) -> Vec<u8>

fn to_hex_len(&mut self, len: usize) -> String

impl DigestExt for Sha1

fn default_len() -> usize

fn to_bytes(&mut self) -> Vec<u8>

fn to_hex(&mut self) -> String

Derived Implementations

impl Clone for Sha1

fn clone(&self) -> Sha1

fn clone_from(&mut self, source: &Self)