|
1 | | -import hello from './index' |
| 1 | +import { |
| 2 | + generateKeys, |
| 3 | + importKeys, |
| 4 | + parsePublicKey, |
| 5 | + parseSecretKey, |
| 6 | + signUtf8String, |
| 7 | + publicKeyRegex, |
| 8 | + secretKeyRegex, |
| 9 | + signatureRegex, |
| 10 | + verifySignature |
| 11 | +} from './index' |
2 | 12 |
|
3 | | -test('testing works', () => { |
4 | | - expect(hello('World')).toEqual('Hello, World !') |
| 13 | +test('regex', () => { |
| 14 | + const keys = generateKeys() |
| 15 | + expect(keys.public).toMatch(publicKeyRegex) |
| 16 | + expect(keys.secret).toMatch(secretKeyRegex) |
| 17 | + expect(keys.public).not.toMatch(secretKeyRegex) |
| 18 | + expect(keys.secret).not.toMatch(publicKeyRegex) |
| 19 | + const message = signUtf8String('', keys.raw.secretKey) |
| 20 | + expect(message).toMatch(signatureRegex) |
| 21 | +}) |
| 22 | + |
| 23 | +test('generate keys', () => { |
| 24 | + const keys = generateKeys() |
| 25 | + expect(parsePublicKey(keys.public)).toEqual(keys.raw.publicKey) |
| 26 | + expect(parseSecretKey(keys.secret)).toEqual(keys.raw.secretKey) |
| 27 | + const copy = importKeys(keys.secret) |
| 28 | + expect(keys.public).toEqual(copy.public) |
| 29 | + expect(keys.secret).toEqual(copy.secret) |
| 30 | + expect(keys.public).not.toEqual(copy.secret) |
| 31 | + expect(keys.secret).not.toEqual(copy.public) |
| 32 | + expect(keys.raw.publicKey).toEqual(copy.raw.publicKey) |
| 33 | + expect(keys.raw.secretKey).toEqual(copy.raw.secretKey) |
| 34 | + expect(keys.raw.publicKey).not.toEqual(copy.raw.secretKey) |
| 35 | + expect(keys.raw.secretKey).not.toEqual(copy.raw.publicKey) |
| 36 | +}) |
| 37 | + |
| 38 | +test('codec', () => { |
| 39 | + const keys = generateKeys() |
| 40 | + const expected = 'Hello, World !' |
| 41 | + const signature = signUtf8String(expected, keys.raw.secretKey) |
| 42 | + const received = verifySignature(signature, keys.raw.publicKey) |
| 43 | + expect(received).toEqual(expected) |
| 44 | +}) |
| 45 | + |
| 46 | +test('Known test vector', () => { |
| 47 | + const secretKey = |
| 48 | + 'ssk.IPwaySrr89g2ymWBrqC81qk7NCmenVN_gFmiz9gtAuTWGhwBv-mSUWbFeS9Zk00Iir8z2GM5Eue4v39FEpOiFw' |
| 49 | + const publicKey = 'spk.1hocAb_pklFmxXkvWZNNCIq_M9hjORLnuL9_RRKTohc' |
| 50 | + const message = |
| 51 | + 'v1.naclsig.5InC2t-TYSFwIFOv-M2nY2zvPYD_ZVExkUqx3bxBYwJSVMvJOZqrJnrUmLuXZsmUHNO6xHX_WdbphwIih_2wD0hlbGxvLCBXb3JsZCAh==' |
| 52 | + const keys = importKeys(secretKey) |
| 53 | + expect(keys.public).toEqual(publicKey) |
| 54 | + const expected = 'Hello, World !' |
| 55 | + const received = verifySignature(message, keys.raw.publicKey) |
| 56 | + expect(received).toEqual(expected) |
| 57 | +}) |
| 58 | + |
| 59 | +test('Known test vector, no padding', () => { |
| 60 | + const secretKey = |
| 61 | + 'ssk.IPwaySrr89g2ymWBrqC81qk7NCmenVN_gFmiz9gtAuTWGhwBv-mSUWbFeS9Zk00Iir8z2GM5Eue4v39FEpOiFw' |
| 62 | + const publicKey = 'spk.1hocAb_pklFmxXkvWZNNCIq_M9hjORLnuL9_RRKTohc' |
| 63 | + const message = |
| 64 | + 'v1.naclsig.5InC2t-TYSFwIFOv-M2nY2zvPYD_ZVExkUqx3bxBYwJSVMvJOZqrJnrUmLuXZsmUHNO6xHX_WdbphwIih_2wD0hlbGxvLCBXb3JsZCAh' |
| 65 | + const keys = importKeys(secretKey) |
| 66 | + expect(keys.public).toEqual(publicKey) |
| 67 | + const expected = 'Hello, World !' |
| 68 | + const received = verifySignature(message, keys.raw.publicKey) |
| 69 | + expect(received).toEqual(expected) |
| 70 | +}) |
| 71 | + |
| 72 | +// Failure cases -- |
| 73 | + |
| 74 | +test('Invalid public key parsing', () => { |
| 75 | + const run = () => parsePublicKey('not a public key') |
| 76 | + expect(run).toThrowError('Invalid public key format') |
| 77 | +}) |
| 78 | + |
| 79 | +test('Invalid secret key parsing', () => { |
| 80 | + const run = () => parseSecretKey('not a secret key') |
| 81 | + expect(run).toThrowError('Invalid secret key format') |
| 82 | +}) |
| 83 | + |
| 84 | +test('Invalid secret key parsing', () => { |
| 85 | + const run = () => parseSecretKey('not a secret key') |
| 86 | + expect(run).toThrowError('Invalid secret key format') |
| 87 | +}) |
| 88 | + |
| 89 | +test('Import keys from invalid secret key', () => { |
| 90 | + const run = () => importKeys('not a secret key') |
| 91 | + expect(run).toThrowError('Invalid secret key format') |
| 92 | +}) |
| 93 | + |
| 94 | +test('Verify signature from wrong public key', () => { |
| 95 | + const publicKey = 'spk.thisisnotthecorrectpublickeyforthismessage_' |
| 96 | + const message = |
| 97 | + 'v1.naclsig.5InC2t-TYSFwIFOv-M2nY2zvPYD_ZVExkUqx3bxBYwJSVMvJOZqrJnrUmLuXZsmUHNO6xHX_WdbphwIih_2wD0hlbGxvLCBXb3JsZCAh' |
| 98 | + const pk = parsePublicKey(publicKey) |
| 99 | + const run = () => verifySignature(message, pk) |
| 100 | + expect(run).toThrowError('Failed to verify signature') |
| 101 | +}) |
| 102 | + |
| 103 | +test('Verify signature with invalid format', () => { |
| 104 | + const publicKey = 'spk.1hocAb_pklFmxXkvWZNNCIq_M9hjORLnuL9_RRKTohc' |
| 105 | + const message = 'not an actual message' |
| 106 | + const pk = parsePublicKey(publicKey) |
| 107 | + const run = () => verifySignature(message, pk) |
| 108 | + expect(run).toThrowError('Invalid secret key format') |
5 | 109 | }) |
0 commit comments