3
3
4
4
import { DefaultLogger } from '.'
5
5
import { decrypt , deriveKeyPbkdf2 , digest , encrypt , randomUint8Array } from './crypto'
6
- import { equalBinary , toHex } from './data/bin'
6
+ import { equalBinary , fromBase64 , toBase64 } from './data/bin'
7
7
8
8
const log = DefaultLogger ( 'crypto.spec' )
9
9
@@ -16,54 +16,15 @@ describe('crypto', () => {
16
16
let id : Uint8Array | undefined
17
17
while ( ( id = list . pop ( ) ) ) {
18
18
// console.log(id)
19
- expect ( equalBinary ( id , new Uint8Array ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ) ) ) . toBe (
20
- false ,
21
- )
19
+ expect ( equalBinary ( id , new Uint8Array ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ) ) ) . toBe ( false )
22
20
expect ( id ?. length ) . toBe ( 8 )
23
21
expect ( list ) . not . toContain ( id )
24
22
}
25
23
} )
26
24
27
25
it ( 'should digest' , async ( ) => {
28
- expect ( toHex ( await digest ( 'abc' ) ) ) . toBe (
29
- 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad' ,
30
- )
31
- expect ( await digest ( new Uint8Array ( [ 1 , 2 , 3 ] ) ) ) . toMatchInlineSnapshot ( `
32
- Uint8Array [
33
- 3,
34
- 144,
35
- 88,
36
- 198,
37
- 242,
38
- 192,
39
- 203,
40
- 73,
41
- 44,
42
- 83,
43
- 59,
44
- 10,
45
- 77,
46
- 20,
47
- 239,
48
- 119,
49
- 204,
50
- 15,
51
- 120,
52
- 171,
53
- 204,
54
- 206,
55
- 213,
56
- 40,
57
- 125,
58
- 132,
59
- 161,
60
- 162,
61
- 1,
62
- 28,
63
- 251,
64
- 129,
65
- ]
66
- ` )
26
+ expect ( toBase64 ( await digest ( 'abc' ) ) ) . toBe ( 'ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0=' )
27
+ expect ( toBase64 ( await digest ( new Uint8Array ( [ 1 , 2 , 3 ] ) ) ) ) . toMatchInlineSnapshot ( `"A5BYxvLAy0ksUzsKTRTvd8wPeKvMztUofYShogEc+4E="` )
67
28
} )
68
29
69
30
// it("should derive key", async () => {
@@ -92,56 +53,12 @@ Uint8Array [
92
53
} )
93
54
const sample = new Uint8Array ( [ 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 ] )
94
55
const cipher = await encrypt ( sample , key )
95
- // log(" cipher", cipher)
56
+ log ( ' cipher' , toBase64 ( cipher ) )
96
57
97
58
const bin = await decrypt ( cipher , key )
98
59
expect ( equalBinary ( sample , bin ) ) . toBe ( true )
99
60
100
- const binFix = await decrypt (
101
- new Uint8Array ( [
102
- 1 ,
103
- 1 ,
104
- 27 ,
105
- 108 ,
106
- 252 ,
107
- 31 ,
108
- 238 ,
109
- 192 ,
110
- 61 ,
111
- 168 ,
112
- 45 ,
113
- 29 ,
114
- 128 ,
115
- 212 ,
116
- 215 ,
117
- 222 ,
118
- 205 ,
119
- 105 ,
120
- 178 ,
121
- 193 ,
122
- 150 ,
123
- 36 ,
124
- 24 ,
125
- 216 ,
126
- 180 ,
127
- 75 ,
128
- 168 ,
129
- 133 ,
130
- 37 ,
131
- 25 ,
132
- 124 ,
133
- 137 ,
134
- 221 ,
135
- 103 ,
136
- 214 ,
137
- 97 ,
138
- 218 ,
139
- 232 ,
140
- 248 ,
141
- 93 ,
142
- ] ) ,
143
- key ,
144
- )
61
+ const binFix = await decrypt ( fromBase64 ( 'AQELynGCxvLXKwLM/oHjOaM4R6d7oAzxJpgpCZnKmWwhkwIDzpPMUQ==' ) , key )
145
62
expect ( binFix ) . toEqual ( sample )
146
63
} )
147
64
0 commit comments