@hg-ts/rsa 0.7.24 → 0.7.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +331 -0
- package/dist/X25519/index.d.ts +4 -0
- package/dist/X25519/index.d.ts.map +1 -0
- package/dist/X25519/index.js +4 -0
- package/dist/X25519/index.js.map +1 -0
- package/dist/X25519/key-pair.d.ts +13 -0
- package/dist/X25519/key-pair.d.ts.map +1 -0
- package/dist/X25519/key-pair.js +39 -0
- package/dist/X25519/key-pair.js.map +1 -0
- package/dist/X25519/private-key.d.ts +11 -0
- package/dist/X25519/private-key.d.ts.map +1 -0
- package/dist/X25519/private-key.js +40 -0
- package/dist/X25519/private-key.js.map +1 -0
- package/dist/X25519/public-key.d.ts +13 -0
- package/dist/X25519/public-key.d.ts.map +1 -0
- package/dist/X25519/public-key.js +49 -0
- package/dist/X25519/public-key.js.map +1 -0
- package/dist/X25519/utils.d.ts +2 -0
- package/dist/X25519/utils.d.ts.map +1 -0
- package/dist/X25519/utils.js +12 -0
- package/dist/X25519/utils.js.map +1 -0
- package/dist/X25519/x25519.test.d.ts +18 -0
- package/dist/X25519/x25519.test.d.ts.map +1 -0
- package/dist/X25519/x25519.test.js +183 -0
- package/dist/X25519/x25519.test.js.map +1 -0
- package/dist/base/index.d.ts +5 -0
- package/dist/base/index.d.ts.map +1 -0
- package/dist/base/index.js +5 -0
- package/dist/base/index.js.map +1 -0
- package/dist/base/key-pair.d.ts +27 -0
- package/dist/base/key-pair.d.ts.map +1 -0
- package/dist/base/key-pair.js +29 -0
- package/dist/base/key-pair.js.map +1 -0
- package/dist/base/key.d.ts +7 -0
- package/dist/base/key.d.ts.map +1 -0
- package/dist/base/key.js +10 -0
- package/dist/base/key.js.map +1 -0
- package/dist/base/private-key.d.ts +11 -0
- package/dist/base/private-key.d.ts.map +1 -0
- package/dist/base/private-key.js +4 -0
- package/dist/base/private-key.js.map +1 -0
- package/dist/base/public-key.d.ts +6 -0
- package/dist/base/public-key.d.ts.map +1 -0
- package/dist/base/public-key.js +4 -0
- package/dist/base/public-key.js.map +1 -0
- package/dist/exceptions/index.d.ts +2 -0
- package/dist/exceptions/index.d.ts.map +1 -0
- package/dist/exceptions/index.js +2 -0
- package/dist/exceptions/index.js.map +1 -0
- package/dist/exceptions/invalid-decryption-key.expection.d.ts +5 -0
- package/dist/exceptions/invalid-decryption-key.expection.d.ts.map +1 -0
- package/dist/exceptions/invalid-decryption-key.expection.js +7 -0
- package/dist/exceptions/invalid-decryption-key.expection.js.map +1 -0
- package/dist/index.d.ts +4 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -3
- package/dist/index.js.map +1 -1
- package/dist/rsa/index.d.ts +4 -0
- package/dist/rsa/index.d.ts.map +1 -0
- package/dist/rsa/index.js +4 -0
- package/dist/rsa/index.js.map +1 -0
- package/dist/rsa/key-pair.d.ts +12 -0
- package/dist/rsa/key-pair.d.ts.map +1 -0
- package/dist/rsa/key-pair.js +40 -0
- package/dist/rsa/key-pair.js.map +1 -0
- package/dist/rsa/private-key.d.ts +16 -0
- package/dist/rsa/private-key.d.ts.map +1 -0
- package/dist/rsa/private-key.js +65 -0
- package/dist/rsa/private-key.js.map +1 -0
- package/dist/rsa/public-key.d.ts +19 -0
- package/dist/rsa/public-key.d.ts.map +1 -0
- package/dist/rsa/public-key.js +85 -0
- package/dist/rsa/public-key.js.map +1 -0
- package/dist/{rsa.test.d.ts → rsa/rsa.test.d.ts} +6 -0
- package/dist/rsa/rsa.test.d.ts.map +1 -0
- package/dist/{rsa.test.js → rsa/rsa.test.js} +101 -24
- package/dist/rsa/rsa.test.js.map +1 -0
- package/package.json +11 -9
- package/src/X25519/index.ts +3 -0
- package/src/X25519/key-pair.ts +58 -0
- package/src/X25519/private-key.ts +54 -0
- package/src/X25519/public-key.ts +68 -0
- package/src/X25519/utils.ts +22 -0
- package/src/X25519/x25519.test.ts +150 -0
- package/src/base/index.ts +4 -0
- package/src/base/key-pair.ts +75 -0
- package/src/base/key.ts +13 -0
- package/src/base/private-key.ts +17 -0
- package/src/base/public-key.ts +7 -0
- package/src/exceptions/index.ts +1 -0
- package/src/exceptions/invalid-decryption-key.expection.ts +7 -0
- package/src/index.ts +4 -3
- package/src/rsa/index.ts +3 -0
- package/src/rsa/key-pair.ts +55 -0
- package/src/rsa/private-key.ts +82 -0
- package/src/rsa/public-key.ts +110 -0
- package/src/rsa/rsa.test.ts +194 -0
- package/dist/rsa.base-key.d.ts +0 -16
- package/dist/rsa.base-key.d.ts.map +0 -1
- package/dist/rsa.base-key.js +0 -54
- package/dist/rsa.base-key.js.map +0 -1
- package/dist/rsa.key-pair.d.ts +0 -19
- package/dist/rsa.key-pair.d.ts.map +0 -1
- package/dist/rsa.key-pair.js +0 -46
- package/dist/rsa.key-pair.js.map +0 -1
- package/dist/rsa.private-key.d.ts +0 -13
- package/dist/rsa.private-key.d.ts.map +0 -1
- package/dist/rsa.private-key.js +0 -38
- package/dist/rsa.private-key.js.map +0 -1
- package/dist/rsa.public-key.d.ts +0 -13
- package/dist/rsa.public-key.d.ts.map +0 -1
- package/dist/rsa.public-key.js +0 -52
- package/dist/rsa.public-key.js.map +0 -1
- package/dist/rsa.test.d.ts.map +0 -1
- package/dist/rsa.test.js.map +0 -1
- package/src/rsa.base-key.ts +0 -75
- package/src/rsa.key-pair.ts +0 -65
- package/src/rsa.private-key.ts +0 -50
- package/src/rsa.public-key.ts +0 -65
- package/src/rsa.test.ts +0 -134
package/src/rsa.private-key.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import forge from 'node-forge';
|
|
2
|
-
import { RSABaseKey } from './rsa.base-key.js';
|
|
3
|
-
import { RSAPublicKey } from './rsa.public-key.js';
|
|
4
|
-
|
|
5
|
-
export class RSAPrivateKey extends RSABaseKey {
|
|
6
|
-
protected readonly key: forge.pki.rsa.PrivateKey;
|
|
7
|
-
|
|
8
|
-
public constructor(key: forge.pki.rsa.PrivateKey) {
|
|
9
|
-
super();
|
|
10
|
-
this.key = key;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
public toPublicKey(): RSAPublicKey {
|
|
14
|
-
return new RSAPublicKey(forge.pki.rsa.setPublicKey(this.key.n, this.key.e));
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
public decrypt(encrypted: string | Buffer): string {
|
|
18
|
-
const md = this.getMd();
|
|
19
|
-
|
|
20
|
-
const chunks = this.prepareToDecrypt(encrypted);
|
|
21
|
-
|
|
22
|
-
const decryptedChunks = chunks.map(chunk => this.key.decrypt(chunk, 'RSAES-PKCS1-V1_5', { md }));
|
|
23
|
-
|
|
24
|
-
return decryptedChunks.join('');
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
public sign(value: string): Buffer {
|
|
28
|
-
const hash = this.getMd();
|
|
29
|
-
hash.update(value);
|
|
30
|
-
|
|
31
|
-
const pss = forge.pss.create({
|
|
32
|
-
md: this.getMd(),
|
|
33
|
-
mgf: forge.mgf.mgf1.create(this.getMd()),
|
|
34
|
-
saltLength: 20,
|
|
35
|
-
});
|
|
36
|
-
const signedId = this.key.sign(hash, pss);
|
|
37
|
-
|
|
38
|
-
return Buffer.from(signedId, 'binary');
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
public override toString(): string {
|
|
42
|
-
return forge.pki.privateKeyToPem(this.key);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
public static fromString(pemKey: string): RSAPrivateKey {
|
|
46
|
-
const key = forge.pki.privateKeyFromPem(pemKey);
|
|
47
|
-
|
|
48
|
-
return new RSAPrivateKey(key);
|
|
49
|
-
}
|
|
50
|
-
}
|
package/src/rsa.public-key.ts
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import { z } from '@hg-ts/validation';
|
|
2
|
-
import forge from 'node-forge';
|
|
3
|
-
import { RSABaseKey } from './rsa.base-key.js';
|
|
4
|
-
|
|
5
|
-
const schema = z.string().transform((value, ctx) => {
|
|
6
|
-
try {
|
|
7
|
-
const publicKey = forge.pki.publicKeyFromPem(value);
|
|
8
|
-
|
|
9
|
-
return forge.pki.publicKeyToPem(publicKey);
|
|
10
|
-
} catch (error) {
|
|
11
|
-
ctx.issues.push({
|
|
12
|
-
message: 'Invalid public key',
|
|
13
|
-
fatal: true,
|
|
14
|
-
code: 'invalid_format',
|
|
15
|
-
input: value,
|
|
16
|
-
format: 'RSA Public Key',
|
|
17
|
-
});
|
|
18
|
-
return value;
|
|
19
|
-
}
|
|
20
|
-
}).pipe(z.string());
|
|
21
|
-
|
|
22
|
-
export class RSAPublicKey extends RSABaseKey {
|
|
23
|
-
protected readonly key: forge.pki.rsa.PublicKey;
|
|
24
|
-
|
|
25
|
-
public static schema = schema;
|
|
26
|
-
|
|
27
|
-
public constructor(key: forge.pki.rsa.PublicKey) {
|
|
28
|
-
super();
|
|
29
|
-
this.key = key;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
public encrypt(value: string | Buffer): Buffer {
|
|
33
|
-
const md = this.getMd();
|
|
34
|
-
const chunks = this.prepareToEncrypt(value);
|
|
35
|
-
|
|
36
|
-
const encryptedChunks = chunks.map(chunk => this.key.encrypt(chunk, 'RSAES-PKCS1-V1_5', { md }));
|
|
37
|
-
|
|
38
|
-
return this.formatEncrypted(encryptedChunks);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
public verify(signature: string | Buffer, value: string): boolean {
|
|
42
|
-
const hash = this.getMd();
|
|
43
|
-
hash.update(value);
|
|
44
|
-
|
|
45
|
-
const pss = forge.pss.create({
|
|
46
|
-
md: this.getMd(),
|
|
47
|
-
mgf: forge.mgf.mgf1.create(this.getMd()),
|
|
48
|
-
saltLength: 20,
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
const formattedSignature = this.formatSignature(signature);
|
|
52
|
-
|
|
53
|
-
return this.key.verify(hash.digest().bytes(), formattedSignature, pss);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
public override toString(): string {
|
|
57
|
-
return forge.pki.publicKeyToPem(this.key);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
public static fromString(pemKey: string): RSAPublicKey {
|
|
61
|
-
const key = forge.pki.publicKeyFromPem(pemKey);
|
|
62
|
-
|
|
63
|
-
return new RSAPublicKey(key);
|
|
64
|
-
}
|
|
65
|
-
}
|
package/src/rsa.test.ts
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Describe,
|
|
3
|
-
expect,
|
|
4
|
-
ExpectException,
|
|
5
|
-
Suite,
|
|
6
|
-
Test,
|
|
7
|
-
} from '@hg-ts/tests';
|
|
8
|
-
import { z } from '@hg-ts/validation';
|
|
9
|
-
|
|
10
|
-
import { RSAKeyPair } from './rsa.key-pair.js';
|
|
11
|
-
import { RSAPrivateKey } from './rsa.private-key.js';
|
|
12
|
-
import { RSAPublicKey } from './rsa.public-key.js';
|
|
13
|
-
|
|
14
|
-
@Describe()
|
|
15
|
-
export class RsaTest extends Suite {
|
|
16
|
-
@Test()
|
|
17
|
-
public async keyPairFromPrivateKey(): Promise<void> {
|
|
18
|
-
const { privateKey, publicKey } = new RSAKeyPair({ bits: 512 });
|
|
19
|
-
const key = RSAPrivateKey.fromString(privateKey);
|
|
20
|
-
|
|
21
|
-
const keyPairFromKey = new RSAKeyPair({ privateKey: key });
|
|
22
|
-
|
|
23
|
-
expect(keyPairFromKey.privateKey).toBe(privateKey);
|
|
24
|
-
expect(keyPairFromKey.publicKey).toBe(publicKey);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
@Test()
|
|
28
|
-
public async keyPairFromPrivateKeyString(): Promise<void> {
|
|
29
|
-
const { privateKey, publicKey } = new RSAKeyPair({ bits: 512 });
|
|
30
|
-
|
|
31
|
-
const keyPairFromKey = new RSAKeyPair({ privateKey });
|
|
32
|
-
|
|
33
|
-
expect(keyPairFromKey.privateKey).toBe(privateKey);
|
|
34
|
-
expect(keyPairFromKey.publicKey).toBe(publicKey);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
@Test()
|
|
38
|
-
public async signature(): Promise<void> {
|
|
39
|
-
const rsa = new RSAKeyPair({ bits: 512 });
|
|
40
|
-
const value = Math.random().toString();
|
|
41
|
-
|
|
42
|
-
const signature = rsa.sign(value);
|
|
43
|
-
expect(rsa.verify(signature.toString('binary'), value)).toBeTruthy();
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
@Test()
|
|
47
|
-
public async signatureBuffer(): Promise<void> {
|
|
48
|
-
const rsa = new RSAKeyPair({ bits: 512 });
|
|
49
|
-
const value = Math.random().toString();
|
|
50
|
-
|
|
51
|
-
const signature = rsa.sign(value);
|
|
52
|
-
expect(rsa.verify(signature, value)).toBeTruthy();
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
@Test()
|
|
56
|
-
public async encryption(): Promise<void> {
|
|
57
|
-
const rsa = new RSAKeyPair({ bits: 1024 });
|
|
58
|
-
const value = Math.random().toString();
|
|
59
|
-
|
|
60
|
-
const encrypted = rsa.encrypt(value);
|
|
61
|
-
expect(rsa.decrypt(encrypted.toString('hex'))).toBe(value);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
@Test()
|
|
65
|
-
public async encryptionBuffer(): Promise<void> {
|
|
66
|
-
const rsa = new RSAKeyPair({ bits: 1024 });
|
|
67
|
-
const value = Buffer.from(Math.random().toString(), 'utf-8');
|
|
68
|
-
|
|
69
|
-
const encrypted = rsa.encrypt(value);
|
|
70
|
-
expect(Buffer.from(rsa.decrypt(encrypted), 'hex')).toMatchObject(value);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
@Test()
|
|
74
|
-
public async longMessageEncryption(): Promise<void> {
|
|
75
|
-
const rsa = new RSAKeyPair({ bits: 1024 });
|
|
76
|
-
const rsaToTest = new RSAKeyPair({ bits: 1024 });
|
|
77
|
-
const value = rsaToTest.publicKey;
|
|
78
|
-
|
|
79
|
-
const encrypted = rsa.encrypt(value);
|
|
80
|
-
expect(rsa.decrypt(encrypted)).toBe(value);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
@Test()
|
|
84
|
-
public async longMessageSignature(): Promise<void> {
|
|
85
|
-
const rsa = new RSAKeyPair({ bits: 512 });
|
|
86
|
-
const value = rsa.publicKey;
|
|
87
|
-
|
|
88
|
-
const signature = rsa.sign(value);
|
|
89
|
-
expect(rsa.verify(signature, value)).toBeTruthy();
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
@Test()
|
|
93
|
-
public async seeded(): Promise<void> {
|
|
94
|
-
const seed = Math.random().toString();
|
|
95
|
-
const rsa1 = new RSAKeyPair({ seed, bits: 512 });
|
|
96
|
-
const rsa2 = new RSAKeyPair({ seed, bits: 512 });
|
|
97
|
-
|
|
98
|
-
expect(rsa1.privateKey).toBe(rsa2.privateKey);
|
|
99
|
-
expect(rsa1.publicKey).toBe(rsa2.publicKey);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
@Test()
|
|
103
|
-
public async privateKeyFromString(): Promise<void> {
|
|
104
|
-
const rsa = new RSAKeyPair({ bits: 512 });
|
|
105
|
-
const rsaFromString = RSAPrivateKey.fromString(rsa.privateKey);
|
|
106
|
-
|
|
107
|
-
expect(rsaFromString.toString()).toBe(rsa.privateKey.toString());
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
@Test()
|
|
111
|
-
public async publicKeyFromString(): Promise<void> {
|
|
112
|
-
const rsa = new RSAKeyPair({ bits: 512 });
|
|
113
|
-
const rsaFromString = RSAPublicKey.fromString(rsa.publicKey);
|
|
114
|
-
|
|
115
|
-
expect(rsaFromString.toString()).toBe(rsa.publicKey.toString());
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
@Test()
|
|
119
|
-
public async publicKeyValidationsValid(): Promise<void> {
|
|
120
|
-
const rsa = new RSAKeyPair({ bits: 512 });
|
|
121
|
-
|
|
122
|
-
RSAPublicKey.schema.parse(rsa.publicKey.toString());
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
@Test()
|
|
126
|
-
@ExpectException(z.ZodError)
|
|
127
|
-
public async publicKeyValidationsFails(): Promise<void> {
|
|
128
|
-
// Тут убран одир случайный символ из ключа
|
|
129
|
-
RSAPublicKey.schema.parse(`-----BEGIN PUBLIC KEY-----
|
|
130
|
-
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALnfPJr6J2UXwvZhbPWolBw4UJHAEMd
|
|
131
|
-
/FvIyIYADhT/k+2TIFixs4pxM5VaGMP7Tny+5WAouv9ulh4tACPxKoMCAwEAAQ==
|
|
132
|
-
-----END PUBLIC KEY-----`);
|
|
133
|
-
}
|
|
134
|
-
}
|