-
Notifications
You must be signed in to change notification settings - Fork 371
/
test.crypto.ts
154 lines (135 loc) Β· 6.08 KB
/
test.crypto.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as base64js from 'base64-js';
import {assert} from 'chai';
import {createCrypto, fromArrayBufferToHex} from '../src/crypto/crypto';
import {BrowserCrypto} from '../src/crypto/browser/crypto';
import {privateKey, publicKey} from './fixtures/keys';
import {describe, it} from 'mocha';
// Not all browsers support `TextEncoder`. The following `require` will
// provide a fast UTF8-only replacement for those browsers that don't support
// text encoding natively.
require('fast-text-encoding');
describe('Browser crypto tests', () => {
const crypto = createCrypto();
it('should create a BrowserCrypto instance', () => {
assert(crypto instanceof BrowserCrypto);
});
it('should calculate SHA256 digest', async () => {
const input = 'I can calculate SHA256';
const expectedDigest = 'c9CEhti/1PtLwS3YkDYE3b3lrZW276VnvXI86BqIESI=';
const calculatedDigest = await crypto.sha256DigestBase64(input);
assert.strictEqual(calculatedDigest, expectedDigest);
});
it('should generate random bytes', () => {
const requestedLength = 20;
const generated1Base64 = crypto.randomBytesBase64(requestedLength);
const generated1 = base64js.toByteArray(generated1Base64);
assert.strictEqual(generated1.length, requestedLength);
const generated2Base64 = crypto.randomBytesBase64(requestedLength);
const generated2 = base64js.toByteArray(generated2Base64);
assert.strictEqual(generated2.length, requestedLength);
// random strings are random! let's just check they are different.
// if they are the same, we have a problem.
assert.notStrictEqual(generated1Base64, generated2Base64);
});
it('should verify a signature', async () => {
const message = 'This message is signed';
const signatureBase64 = [
'BE1qD48LdssePdMmOhcanOd8V+i4yLSOL0H2EXNyy',
'lCePnldIsLVqrOJnVkd0MUKxS/Y9B0te2tqlS8psP',
'j9IWjcpiQeT9wUDRadxHIX26W6JHgSCOzOavpJCbh',
'M3Kez7QEwbkrI54rYu7qgx/mmckxkC0vhg0Z5OQbO',
'IXfILVs1ztNNdt9r/ZzNVxTMKhL3nHLfjVqG/LUGy',
'RhFhjzLvIJAfL0CSEfycUvm6t5NVzF4SkZ8KKQ7wJ',
'vLw492bRB/633GJOZ1prVjAUQUI64BXFrvRgWsxLK',
'M0XtF5tNbC+eIDrH0LiMraAhcZwj1iWofH1h/dg3E',
'xtU9UWfbed/yfw',
].join(''); // note: no padding
const verified = await crypto.verify(publicKey, message, signatureBase64);
assert(verified);
});
it('should sign a message', async () => {
const message = 'This message is signed';
const expectedSignatureBase64 = [
'BE1qD48LdssePdMmOhcanOd8V+i4yLSOL0H2EXNyy',
'lCePnldIsLVqrOJnVkd0MUKxS/Y9B0te2tqlS8psP',
'j9IWjcpiQeT9wUDRadxHIX26W6JHgSCOzOavpJCbh',
'M3Kez7QEwbkrI54rYu7qgx/mmckxkC0vhg0Z5OQbO',
'IXfILVs1ztNNdt9r/ZzNVxTMKhL3nHLfjVqG/LUGy',
'RhFhjzLvIJAfL0CSEfycUvm6t5NVzF4SkZ8KKQ7wJ',
'vLw492bRB/633GJOZ1prVjAUQUI64BXFrvRgWsxLK',
'M0XtF5tNbC+eIDrH0LiMraAhcZwj1iWofH1h/dg3E',
'xtU9UWfbed/yfw==',
].join('');
const signatureBase64 = await crypto.sign(privateKey, message);
assert.strictEqual(signatureBase64, expectedSignatureBase64);
});
it('should decode unpadded base64', () => {
const originalString = 'test string';
const base64String = 'dGVzdCBzdHJpbmc';
const decodedString = crypto.decodeBase64StringUtf8(base64String);
assert.strictEqual(decodedString, originalString);
});
it('should encode to base64 and pad the result', () => {
const originalString = 'test string';
const base64String = 'dGVzdCBzdHJpbmc=';
const encodedString = crypto.encodeBase64StringUtf8(originalString);
assert.strictEqual(encodedString, base64String);
});
it('should calculate SHA256 digest in hex encoding', async () => {
const input = 'I can calculate SHA256';
const expectedHexDigest =
'73d08486d8bfd4fb4bc12dd8903604ddbde5ad95b6efa567bd723ce81a881122';
const calculatedHexDigest = await crypto.sha256DigestHex(input);
assert.strictEqual(calculatedHexDigest, expectedHexDigest);
});
describe('should compute the HMAC-SHA256 hash of a message', () => {
it('using a string key', async () => {
const message = 'The quick brown fox jumps over the lazy dog';
const key = 'key';
const expectedHexHash =
'f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8';
const expectedHash = new Uint8Array(
(expectedHexHash.match(/.{1,2}/g) as string[]).map(byte =>
parseInt(byte, 16)
)
);
const calculatedHash = await crypto.signWithHmacSha256(key, message);
assert.deepStrictEqual(calculatedHash, expectedHash.buffer);
});
it('using an ArrayBuffer key', async () => {
const message = 'The quick brown fox jumps over the lazy dog';
// String "key" ArrayBuffer representation.
const key = new Uint8Array([107, 0, 101, 0, 121, 0])
.buffer as ArrayBuffer;
const expectedHexHash =
'f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8';
const expectedHash = new Uint8Array(
(expectedHexHash.match(/.{1,2}/g) as string[]).map(byte =>
parseInt(byte, 16)
)
);
const calculatedHash = await crypto.signWithHmacSha256(key, message);
assert.deepStrictEqual(calculatedHash, expectedHash.buffer);
});
});
it('should expose a method to convert an ArrayBuffer to hex', () => {
const arrayBuffer = new Uint8Array([4, 8, 0, 12, 16, 0])
.buffer as ArrayBuffer;
const expectedHexEncoding = '0408000c1000';
const calculatedHexEncoding = fromArrayBufferToHex(arrayBuffer);
assert.strictEqual(calculatedHexEncoding, expectedHexEncoding);
});
});