11var Crypto = require ( 'crypto-js' )
22var WordArray = Crypto . lib . WordArray
3- var base64map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
43
54function lpad ( str , padString , length ) {
65 while ( str . length < length ) str = padString + str
@@ -24,78 +23,6 @@ function hexToBytes(hex) {
2423 } )
2524}
2625
27- function bytesToBase64 ( bytes ) {
28- var base64 = [ ]
29-
30- for ( var i = 0 ; i < bytes . length ; i += 3 ) {
31- var triplet = ( bytes [ i ] << 16 ) | ( bytes [ i + 1 ] << 8 ) | bytes [ i + 2 ]
32-
33- for ( var j = 0 ; j < 4 ; j ++ ) {
34- if ( i * 8 + j * 6 <= bytes . length * 8 ) {
35- base64 . push ( base64map . charAt ( ( triplet >>> 6 * ( 3 - j ) ) & 0x3F ) )
36- } else {
37- base64 . push ( '=' )
38- }
39- }
40- }
41-
42- return base64 . join ( '' )
43- }
44-
45- function base64ToBytes ( base64 ) {
46- // Remove non-base-64 characters
47- base64 = base64 . replace ( / [ ^ A - Z 0 - 9 + \/ ] / ig, '' )
48-
49- var bytes = [ ]
50- var imod4 = 0
51-
52- for ( var i = 0 ; i < base64 . length ; imod4 = ++ i % 4 ) {
53- if ( ! imod4 ) continue
54-
55- bytes . push (
56- (
57- ( base64map . indexOf ( base64 . charAt ( i - 1 ) ) & ( Math . pow ( 2 , - 2 * imod4 + 8 ) - 1 ) ) <<
58- ( imod4 * 2 )
59- ) |
60- ( base64map . indexOf ( base64 . charAt ( i ) ) >>> ( 6 - imod4 * 2 ) )
61- )
62- }
63-
64- return bytes
65- }
66-
67- /**
68- * Hex only (allowing bin would be potentially risky, as 01010101 = \x01 * 4 or 85)
69- */
70- function coerceToBytes ( input ) {
71- if ( typeof input != 'string' ) return input
72- return hexToBytes ( input )
73- }
74-
75- function binToBytes ( bin ) {
76- return bin . match ( / ......../ g) . map ( function ( x ) {
77- return parseInt ( x , 2 )
78- } )
79- }
80-
81- function bytesToBin ( bytes ) {
82- return bytes . map ( function ( x ) {
83- return lpad ( x . toString ( 2 ) , '0' , 8 )
84- } ) . join ( '' )
85- }
86-
87- function bytesToString ( bytes ) {
88- return bytes . map ( function ( x ) {
89- return String . fromCharCode ( x )
90- } ) . join ( '' )
91- }
92-
93- function stringToBytes ( string ) {
94- return string . split ( '' ) . map ( function ( x ) {
95- return x . charCodeAt ( 0 )
96- } )
97- }
98-
9926/**
10027 * Create a byte array representing a number with the given length
10128 */
@@ -181,13 +108,6 @@ module.exports = {
181108 lpad : lpad ,
182109 bytesToHex : bytesToHex ,
183110 hexToBytes : hexToBytes ,
184- bytesToBase64 : bytesToBase64 ,
185- base64ToBytes : base64ToBytes ,
186- coerceToBytes : coerceToBytes ,
187- binToBytes : binToBytes ,
188- bytesToBin : bytesToBin ,
189- bytesToString : bytesToString ,
190- stringToBytes : stringToBytes ,
191111 numToBytes : numToBytes ,
192112 bytesToNum : bytesToNum ,
193113 numToVarInt : numToVarInt ,
0 commit comments