1
|
|
|
const fs = require('fs'); |
2
|
|
|
const path = require('path'); |
3
|
|
|
const dsa = require('../../lib/cjs'); |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
describe('Digital Signature Authentication', () => { |
7
|
|
|
beforeEach(() => { |
8
|
|
|
fs.mkdirSync(path.join(__dirname, './../tmp'), { recursive: true }); |
9
|
|
|
dsa.init({ |
10
|
|
|
modulusLength: 2048, |
11
|
|
|
storageLocalPath: path.join(__dirname, './../tmp'), |
12
|
|
|
}); |
13
|
|
|
}); |
14
|
|
|
|
15
|
|
|
afterEach(() => { |
16
|
|
|
fs.rmdirSync(path.join(__dirname, './../tmp'), { recursive: true }); |
17
|
|
|
}); |
18
|
|
|
|
19
|
|
|
describe('generateKeys', () => { |
20
|
|
|
it('should generate public and private keys', () => { |
21
|
|
|
const keyName = 'testKey'; |
22
|
|
|
const { publicKey, privateKey } = dsa.generateKeys(keyName); |
23
|
|
|
|
24
|
|
|
const publicKeyBuffer = Buffer.from(publicKey, 'utf-8'); |
|
|
|
|
25
|
|
|
const privateKeyBuffer = Buffer.from(privateKey, 'utf-8'); |
26
|
|
|
|
27
|
|
|
expect(publicKeyBuffer).toBeInstanceOf(Buffer); |
28
|
|
|
expect(privateKeyBuffer).toBeInstanceOf(Buffer); |
29
|
|
|
}); |
30
|
|
|
}); |
31
|
|
|
|
32
|
|
|
describe('createSign and verifySign', () => { |
33
|
|
|
it('should create and verify a valid digital signature', () => { |
34
|
|
|
const keyName = 'testKey'; |
35
|
|
|
const { publicKey, privateKey } = dsa.generateKeys(keyName); |
36
|
|
|
|
37
|
|
|
const dataToSign = 'Hello, world!'; |
38
|
|
|
const signature = dsa.createSign(dataToSign, privateKey); |
39
|
|
|
|
40
|
|
|
// Verify signature |
41
|
|
|
const isVerified = dsa.verifySign({ signature, data: dataToSign }, publicKey); |
42
|
|
|
expect(isVerified).toBe(true); |
43
|
|
|
}); |
44
|
|
|
|
45
|
|
|
it('should fail to verify an invalid digital signature', () => { |
46
|
|
|
const keyName = 'testKey'; |
47
|
|
|
const { publicKey, privateKey } = dsa.generateKeys(keyName); |
48
|
|
|
|
49
|
|
|
const dataToSign = 'Hello, world!'; |
50
|
|
|
const maliciousData = 'Hello, attacker!'; |
51
|
|
|
|
52
|
|
|
const signature = dsa.createSign(dataToSign, privateKey); |
53
|
|
|
|
54
|
|
|
// Verify with malicious data |
55
|
|
|
const isVerified = dsa.verifySign({ signature, data: maliciousData }, publicKey); |
56
|
|
|
expect(isVerified).toBe(false); |
57
|
|
|
}); |
58
|
|
|
}); |
59
|
|
|
}); |
60
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.