1414 */
1515
1616macro_rules! hash_func {
17- ( $ctx: expr, $update: expr, $convert: expr) => {
18- use std:: io:: Read ;
17+ ( $ctx: expr, $update: expr, $convert: expr) => {
18+ use std:: io:: Read ;
1919
20- pub fn hash<R : Read >( reader: & mut R ) -> String {
21- let mut buffer = vec![ 0 ; 4096 ] ;
20+ pub fn hash<R : Read >( reader: & mut R ) -> String {
21+ let mut buffer = vec![ 0 ; 4096 ] ;
2222
23- let mut ctx = $ctx;
24- loop {
25- let read = reader. read( & mut buffer[ ..] ) . unwrap( ) ;
23+ let mut ctx = $ctx;
24+ loop {
25+ let read = reader. read( & mut buffer[ ..] ) . unwrap( ) ;
2626
27- if read == 0 {
28- break ;
29- }
27+ if read == 0 {
28+ break ;
29+ }
3030
31- $update( & mut ctx, & buffer[ ..read] ) ;
32- }
31+ $update( & mut ctx, & buffer[ ..read] ) ;
32+ }
3333
34- $convert( ctx)
35- }
36- } ;
34+ $convert( ctx)
35+ }
36+ } ;
3737}
3838
3939macro_rules! hash_func_write {
40- ( $ctx: expr, $convert: expr) => {
41- use std:: io:: { self , Read } ;
40+ ( $ctx: expr, $convert: expr) => {
41+ use std:: io:: { self , Read } ;
4242
43- pub fn hash<R : Read >( reader: & mut R ) -> String {
44- let mut ctx = $ctx;
45- io:: copy( reader, & mut ctx) . unwrap( ) ;
46- $convert( ctx)
47- }
48- } ;
43+ pub fn hash<R : Read >( reader: & mut R ) -> String {
44+ let mut ctx = $ctx;
45+ io:: copy( reader, & mut ctx) . unwrap( ) ;
46+ $convert( ctx)
47+ }
48+ } ;
4949}
5050
51+ use std:: { fmt:: Write , fs:: File , io:: Read , path:: Path } ;
52+
5153use super :: Algorithm ;
52- use std:: fmt:: Write ;
53- use std:: fs:: File ;
54- use std:: io:: Read ;
55- use std:: path:: Path ;
5654
5755mod blake2b;
5856mod blake2s;
@@ -75,45 +73,48 @@ mod xxh64;
7573
7674/// Hash the specified file using the specified hashing algorithm.
7775pub fn hash_file ( algo : Algorithm , path : & Path ) -> String {
78- hash_reader ( algo, & mut File :: open ( path) . unwrap ( ) )
76+ hash_reader ( algo, & mut File :: open ( path) . unwrap ( ) )
7977}
8078
8179/// Hash the specified byte stream using the specified hashing algorithm.
8280pub fn hash_reader < R : Read > ( algo : Algorithm , data : & mut R ) -> String {
83- match algo {
84- Algorithm :: CRC32 => crc32:: hash ( data) ,
85- Algorithm :: SHA1 => sha1:: hash ( data) ,
86- Algorithm :: SHA2224 => sha2_224:: hash ( data) ,
87- Algorithm :: SHA2256 => sha2_256:: hash ( data) ,
88- Algorithm :: SHA2384 => sha2_384:: hash ( data) ,
89- Algorithm :: SHA2512 => sha2_512:: hash ( data) ,
90- Algorithm :: SHA3224 => sha3_224:: hash ( data) ,
91- Algorithm :: SHA3256 => sha3_256:: hash ( data) ,
92- Algorithm :: SHA3384 => sha3_384:: hash ( data) ,
93- Algorithm :: SHA3512 => sha3_512:: hash ( data) ,
94- Algorithm :: MD5 => md5:: hash ( data) ,
95- Algorithm :: XXH64 => xxh64:: hash ( data) ,
96- Algorithm :: XXH32 => xxh32:: hash ( data) ,
97- Algorithm :: XXH3 => xxh3:: hash ( data) ,
98- Algorithm :: BLAKE2B => blake2b:: hash ( data) ,
99- Algorithm :: BLAKE2S => blake2s:: hash ( data) ,
100- Algorithm :: BLAKE3 => blake3:: hash ( data) ,
101- Algorithm :: WhirlPool => whirlpool:: hash ( data) ,
102- }
81+ match algo {
82+ Algorithm :: CRC32 => crc32:: hash ( data) ,
83+ Algorithm :: SHA1 => sha1:: hash ( data) ,
84+ Algorithm :: SHA2224 => sha2_224:: hash ( data) ,
85+ Algorithm :: SHA2256 => sha2_256:: hash ( data) ,
86+ Algorithm :: SHA2384 => sha2_384:: hash ( data) ,
87+ Algorithm :: SHA2512 => sha2_512:: hash ( data) ,
88+ Algorithm :: SHA3224 => sha3_224:: hash ( data) ,
89+ Algorithm :: SHA3256 => sha3_256:: hash ( data) ,
90+ Algorithm :: SHA3384 => sha3_384:: hash ( data) ,
91+ Algorithm :: SHA3512 => sha3_512:: hash ( data) ,
92+ Algorithm :: MD5 => md5:: hash ( data) ,
93+ Algorithm :: XXH64 => xxh64:: hash ( data) ,
94+ Algorithm :: XXH32 => xxh32:: hash ( data) ,
95+ Algorithm :: XXH3 => xxh3:: hash ( data) ,
96+ Algorithm :: BLAKE2B => blake2b:: hash ( data) ,
97+ Algorithm :: BLAKE2S => blake2s:: hash ( data) ,
98+ Algorithm :: BLAKE3 => blake3:: hash ( data) ,
99+ Algorithm :: WhirlPool => whirlpool:: hash ( data) ,
100+ }
103101}
104102
105103/// Create a hash string out of its raw bytes.
106104///
107105/// # Examples
108106///
109107/// ```
110- /// assert_eq!(quickdash::hash_string(&[0x99, 0xAA, 0xBB, 0xCC]), "99AABBCC".to_string());
108+ /// assert_eq!(
109+ /// quickdash::hash_string(&[0x99, 0xAA, 0xBB, 0xCC]),
110+ /// "99AABBCC".to_string()
111+ /// );
111112/// assert_eq!(quickdash::hash_string(&[0x09, 0x0A]), "090A".to_string());
112113/// ```
113114pub fn hash_string ( bytes : & [ u8 ] ) -> String {
114- let mut result = String :: with_capacity ( bytes. len ( ) * 2 ) ;
115- for b in bytes {
116- write ! ( result, "{:02X}" , b) . unwrap ( ) ;
117- }
118- result
115+ let mut result = String :: with_capacity ( bytes. len ( ) * 2 ) ;
116+ for b in bytes {
117+ write ! ( result, "{:02X}" , b) . unwrap ( ) ;
118+ }
119+ result
119120}
0 commit comments