|
| 1 | +/** |
| 2 | + * Copyright 2019 Google LLC. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
1 | 17 | import * as base64js from 'base64-js'; |
2 | 18 | import {assert} from 'chai'; |
3 | 19 | import {createCrypto} from '../src/crypto/crypto'; |
4 | 20 | import {BrowserCrypto} from '../src/crypto/browser/crypto'; |
5 | | - |
6 | | -// The following public key was copied from JWK RFC 7517: |
7 | | -// https://tools.ietf.org/html/rfc7517 |
8 | | -// The private key used for signing the test message below was taken from the same RFC. |
9 | | -const publicKey = { |
10 | | - kty: 'RSA', |
11 | | - n: |
12 | | - '0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx4cbbfAAtVT86zwu1RK7aPFFxuhDR1L6tSoc_BJECPebWKRXjBZCiFV4n3oknjhMstn64tZ_2W-5JsGY4Hc5n9yBXArwl93lqt7_RN5w6Cf0h4QyQ5v-65YGjQR0_FDW2QvzqY368QQMicAtaSqzs8KJZgnYb9c7d0zgdAZHzu6qMQvRL5hajrn1n91CbOpbISD08qNLyrdkt-bFTWhAI4vMQFh6WeZu0fM4lFd2NcRwr3XPksINHaQ-G_xBniIqbw0Ls1jF44-csFCur-kEgU8awapJzKnqDKgw', |
13 | | - e: 'AQAB', |
14 | | - alg: 'RS256', |
15 | | - kid: '2011-04-29', |
16 | | -}; |
| 21 | +import {privateKey, publicKey} from './fixtures/keys'; |
17 | 22 |
|
18 | 23 | // Not all browsers support `TextEncoder`. The following `require` will |
19 | 24 | // provide a fast UTF8-only replacement for those browsers that don't support |
@@ -64,10 +69,22 @@ describe('Browser crypto tests', () => { |
64 | 69 | assert(verified); |
65 | 70 | }); |
66 | 71 |
|
67 | | - it('should not createSign', () => { |
68 | | - assert.throws(() => { |
69 | | - crypto.createSign('never worked'); |
70 | | - }); |
| 72 | + it('should sign a message', async () => { |
| 73 | + const message = 'This message is signed'; |
| 74 | + const expectedSignatureBase64 = [ |
| 75 | + 'BE1qD48LdssePdMmOhcanOd8V+i4yLSOL0H2EXNyy', |
| 76 | + 'lCePnldIsLVqrOJnVkd0MUKxS/Y9B0te2tqlS8psP', |
| 77 | + 'j9IWjcpiQeT9wUDRadxHIX26W6JHgSCOzOavpJCbh', |
| 78 | + 'M3Kez7QEwbkrI54rYu7qgx/mmckxkC0vhg0Z5OQbO', |
| 79 | + 'IXfILVs1ztNNdt9r/ZzNVxTMKhL3nHLfjVqG/LUGy', |
| 80 | + 'RhFhjzLvIJAfL0CSEfycUvm6t5NVzF4SkZ8KKQ7wJ', |
| 81 | + 'vLw492bRB/633GJOZ1prVjAUQUI64BXFrvRgWsxLK', |
| 82 | + 'M0XtF5tNbC+eIDrH0LiMraAhcZwj1iWofH1h/dg3E', |
| 83 | + 'xtU9UWfbed/yfw==', |
| 84 | + ].join(''); |
| 85 | + |
| 86 | + const signatureBase64 = await crypto.sign(privateKey, message); |
| 87 | + assert.strictEqual(signatureBase64, expectedSignatureBase64); |
71 | 88 | }); |
72 | 89 |
|
73 | 90 | it('should decode unpadded base64', () => { |
|
0 commit comments