@@ -74,9 +74,10 @@ pub fn hotp(K: &str, C: u64, N: u32, algorithm: &HmacAlgorithm) -> u32 {
7474 // (2)
7575 tracepoint ! ( ) ;
7676 let K = hmac:: SigningKey :: new ( algorithm. to_algorithm ( ) , K . as_ref ( ) ) ;
77- // Swap bytes because of endianess
78- debug ! ( "Counter ({}) is {:?}" , C , C . swap_bytes( ) . to_bytes( ) ) ;
79- let H = hmac:: sign ( & K , & C . swap_bytes ( ) . to_bytes ( ) ) ;
77+ // TODO Use int_to_from_bytes (see Rust PR #51835) when stabilized
78+ // in Rust 1.29. When using it, .swap_bytes() because of endianess
79+ println ! ( "Counter ({}) is {:?}" , C , u64_into_bytes( C ) ) ;
80+ let H = hmac:: sign ( & K , & u64_into_bytes ( C ) ) ;
8081 debug ! (
8182 "Signed digest is {}" ,
8283 H . as_ref( )
@@ -111,7 +112,7 @@ pub fn hotp(K: &str, C: u64, N: u32, algorithm: &HmacAlgorithm) -> u32 {
111112#[ cfg( test) ]
112113mod tests {
113114 use lib:: otp;
114- use ring :: digest ;
115+ use lib :: types :: HmacAlgorithm ;
115116
116117 // Test values provided in RFC 4226
117118 // Base32 for "12345678901234567890";
@@ -124,7 +125,7 @@ mod tests {
124125 fn hotp_rfc_values ( ) {
125126 for value in 0 ..RFC_HOTP_VALUES . len ( ) {
126127 assert_eq ! (
127- otp:: hotp( & RFC_HOTP_SECRET , value as u64 , 6 , & digest :: SHA1 ) ,
128+ otp:: hotp( & RFC_HOTP_SECRET , value as u64 , 6 , & HmacAlgorithm :: SHA1 ) ,
128129 RFC_HOTP_VALUES [ value as usize ]
129130 ) ;
130131 }
@@ -155,7 +156,7 @@ mod tests {
155156 & RFC_TOTP_SECRET_SHA1 ,
156157 8 ,
157158 RFC_TOTP_TIMES [ value] ,
158- & digest :: SHA1
159+ & HmacAlgorithm :: SHA1
159160 ) ,
160161 RFC_TOTP_VALUES_SHA1 [ value as usize ]
161162 ) ;
@@ -178,7 +179,7 @@ mod tests {
178179 & RFC_TOTP_SECRET_SHA256 ,
179180 8 ,
180181 RFC_TOTP_TIMES [ value] ,
181- & digest :: SHA256
182+ & HmacAlgorithm :: SHA256
182183 ) ,
183184 RFC_TOTP_VALUES_SHA256 [ value as usize ]
184185 ) ;
@@ -201,10 +202,26 @@ mod tests {
201202 & RFC_TOTP_SECRET_SHA512 ,
202203 8 ,
203204 RFC_TOTP_TIMES [ value] ,
204- & digest :: SHA512
205+ & HmacAlgorithm :: SHA512
205206 ) ,
206207 RFC_TOTP_VALUES_SHA512 [ value as usize ]
207208 ) ;
208209 }
209210 }
210211}
212+
213+ /// Converts u64 to a u8 array
214+ /// Fallback method waiting for stabilization of int_to_from_bytes
215+ /// (see Rust PR #51835) in Rust 1.29
216+ fn u64_into_bytes ( x : u64 ) -> [ u8 ; 8 ] {
217+ [
218+ ( ( x >> 56 ) & 0xff ) as u8 ,
219+ ( ( x >> 48 ) & 0xff ) as u8 ,
220+ ( ( x >> 40 ) & 0xff ) as u8 ,
221+ ( ( x >> 32 ) & 0xff ) as u8 ,
222+ ( ( x >> 24 ) & 0xff ) as u8 ,
223+ ( ( x >> 16 ) & 0xff ) as u8 ,
224+ ( ( x >> 8 ) & 0xff ) as u8 ,
225+ ( x & 0xff ) as u8 ,
226+ ]
227+ }
0 commit comments