|
2 | 2 |
|
3 | 3 | var crypto = require('crypto'); |
4 | 4 |
|
| 5 | +/** |
| 6 | + * convert an integer to a byte array |
| 7 | + * @param {Integer} num |
| 8 | + * @return {Array} bytes |
| 9 | + */ |
| 10 | +function intToBytes(num) { |
| 11 | + var bytes = []; |
| 12 | + |
| 13 | + for(var i=7 ; i>=0 ; --i) { |
| 14 | + bytes[i] = num & (255); |
| 15 | + num = num >> 8; |
| 16 | + } |
| 17 | + |
| 18 | + return bytes; |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * convert a hex value to a byte array |
| 23 | + * @param {String} hex string of hex to convert to a byte array |
| 24 | + * @return {Array} bytes |
| 25 | + */ |
| 26 | +function hexToBytes(hex) { |
| 27 | + var bytes = []; |
| 28 | + for(var c = 0, C = hex.length; c < C; c += 2) { |
| 29 | + bytes.push(parseInt(hex.substr(c, 2), 16)); |
| 30 | + } |
| 31 | + return bytes; |
| 32 | +} |
| 33 | + |
5 | 34 | var hotp = {}; |
6 | 35 |
|
7 | 36 | /** |
@@ -190,33 +219,3 @@ totp.verify = function(token, key, opt) { |
190 | 219 |
|
191 | 220 | module.exports.hotp = hotp; |
192 | 221 | module.exports.totp = totp; |
193 | | - |
194 | | -/** |
195 | | - * convert an integer to a byte array |
196 | | - * @param {Integer} num |
197 | | - * @return {Array} bytes |
198 | | - */ |
199 | | -var intToBytes = function(num) { |
200 | | - var bytes = []; |
201 | | - |
202 | | - for(var i=7 ; i>=0 ; --i) { |
203 | | - bytes[i] = num & (255); |
204 | | - num = num >> 8; |
205 | | - } |
206 | | - |
207 | | - return bytes; |
208 | | -}; |
209 | | - |
210 | | - |
211 | | -/** |
212 | | - * convert a hex value to a byte array |
213 | | - * @param {String} hex string of hex to convert to a byte array |
214 | | - * @return {Array} bytes |
215 | | - */ |
216 | | -var hexToBytes = function(hex) { |
217 | | - var bytes = []; |
218 | | - for(var c = 0, C = hex.length; c < C; c += 2) { |
219 | | - bytes.push(parseInt(hex.substr(c, 2), 16)); |
220 | | - } |
221 | | - return bytes; |
222 | | -}; |
0 commit comments