Skip to content

sha1::Sha1.result_str() can yield incorrect results #4458

Closed
@pkgw

Description

@pkgw

The problem is that each byte in the SHA1 digest is hexified individually in a way that doesn't include leading zeros: a byte of 0x0a just gets turned into 'a'. So if your digest should be "ab0e" the result is "abe". You can see in this program where the printed strings vary in length:

extern mod std;
use std::sha1;

fn main () { 
    let s = sha1::sha1 ();
    let mut i = 0u8;

    while i < 100 {
        s.input (~[i]);
        io::print (s.result_str () + "\n");
        s.reset ();
        i += 1;
    }
}

Do the Rust standard libraries have a byte-array-to-hex converter somewhere? It seems like that'd be a useful thing to tuck in somewhere. Besides being incorrect for the time being, the SHA1 hex converter also looks like it's probably slower than it needs to be.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions