jsonauthtoken 3.0.2 → 3.0.4-beta-1
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/dist/node.index.d.mts +53 -0
- package/dist/node.index.d.ts +53 -0
- package/dist/node.index.js +767 -0
- package/dist/node.index.mjs +728 -0
- package/dist/types.d-D_Cze-FV.d.mts +32 -0
- package/dist/types.d-D_Cze-FV.d.ts +32 -0
- package/dist/types.d.ts +68 -0
- package/dist/web.index.d.mts +52 -0
- package/dist/web.index.d.ts +52 -0
- package/dist/web.index.js +599 -0
- package/dist/web.index.mjs +571 -0
- package/package.json +36 -5
- package/types.d.ts +54 -53
- package/dist/config/algo.config.d.ts +0 -2
- package/dist/config/algo.config.js +0 -28
- package/dist/config/name.config.d.ts +0 -1
- package/dist/config/name.config.js +0 -4
- package/dist/config/runtime.config.d.ts +0 -1
- package/dist/config/runtime.config.js +0 -15
- package/dist/index.d.ts +0 -34
- package/dist/index.js +0 -133
- package/dist/lib/decoading.lib.d.ts +0 -2
- package/dist/lib/decoading.lib.js +0 -9
- package/dist/lib/encoading.lib.d.ts +0 -2
- package/dist/lib/encoading.lib.js +0 -8
- package/dist/lib/functions.lib.d.ts +0 -12
- package/dist/lib/functions.lib.js +0 -85
- package/dist/lib/timeformat.d.ts +0 -1
- package/dist/lib/timeformat.js +0 -42
- package/dist/runtime/node.runtime.d.ts +0 -31
- package/dist/runtime/node.runtime.js +0 -150
- package/dist/runtime/runtime.d.ts +0 -12
- package/dist/runtime/runtime.js +0 -152
- package/dist/runtime/web.runtime.d.ts +0 -38
- package/dist/runtime/web.runtime.js +0 -180
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
-
};
|
|
38
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.NodeCrypto = void 0;
|
|
40
|
-
const crypto_1 = __importDefault(require("crypto"));
|
|
41
|
-
const functions_lib_1 = require("../lib/functions.lib");
|
|
42
|
-
class NodeCrypto {
|
|
43
|
-
crypto;
|
|
44
|
-
async __init() {
|
|
45
|
-
if (!this.crypto) {
|
|
46
|
-
return await Promise.resolve().then(() => __importStar(require('crypto')));
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
_encrypt(algorithm, key, payload) {
|
|
50
|
-
const iv = crypto_1.default.randomBytes(12);
|
|
51
|
-
const cipher = crypto_1.default.createCipheriv(algorithm, key, iv);
|
|
52
|
-
const data = JSON.stringify(payload);
|
|
53
|
-
let encrypted = cipher.update(data, 'utf8', 'base64');
|
|
54
|
-
encrypted += cipher.final('base64');
|
|
55
|
-
const tag = cipher.getAuthTag().toString('base64');
|
|
56
|
-
return {
|
|
57
|
-
iv: iv.toString('base64'),
|
|
58
|
-
encrypted,
|
|
59
|
-
tag
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
_decrypt(algorithm, key, encryptedData) {
|
|
63
|
-
const { iv, encrypted, tag } = encryptedData;
|
|
64
|
-
const decipher = crypto_1.default.createDecipheriv(algorithm, key, Buffer.from(iv, 'base64'));
|
|
65
|
-
decipher.setAuthTag(Buffer.from(tag, 'base64'));
|
|
66
|
-
let decrypted = decipher.update(encrypted, 'base64', 'utf8');
|
|
67
|
-
decrypted += decipher.final('utf8');
|
|
68
|
-
return JSON.parse(decrypted);
|
|
69
|
-
}
|
|
70
|
-
_rsaPublicKeyGeneration(privateKeyPem) {
|
|
71
|
-
const privateKeyObject = crypto_1.default.createPrivateKey({
|
|
72
|
-
key: privateKeyPem.replace(/\\n/g, '\n'),
|
|
73
|
-
format: 'pem',
|
|
74
|
-
type: 'pkcs8'
|
|
75
|
-
});
|
|
76
|
-
const publicKeyObject = crypto_1.default.createPublicKey(privateKeyObject);
|
|
77
|
-
return publicKeyObject.export({ type: 'spki', format: 'pem' });
|
|
78
|
-
}
|
|
79
|
-
_rsaPrivatePublicKeyGeneration() {
|
|
80
|
-
const { publicKey, privateKey } = crypto_1.default.generateKeyPairSync('rsa', {
|
|
81
|
-
modulusLength: 2048,
|
|
82
|
-
publicKeyEncoding: {
|
|
83
|
-
type: 'spki',
|
|
84
|
-
format: 'pem'
|
|
85
|
-
},
|
|
86
|
-
privateKeyEncoding: {
|
|
87
|
-
type: 'pkcs8',
|
|
88
|
-
format: 'pem'
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
return { privateKey, publicKey };
|
|
92
|
-
}
|
|
93
|
-
async encrypt(algo, key, payload, exp) {
|
|
94
|
-
await this.__init();
|
|
95
|
-
const keyHash = this.crypto.createHash('sha256').update(key).digest();
|
|
96
|
-
const newPayload = { payload: payload, exp: exp };
|
|
97
|
-
const { iv, encrypted, tag } = this._encrypt(algo, keyHash, newPayload);
|
|
98
|
-
return (0, functions_lib_1.tokenFormatCreate)({
|
|
99
|
-
runtime: 'node',
|
|
100
|
-
algo: 'AES-256-GCM',
|
|
101
|
-
type: 'symmetric',
|
|
102
|
-
v: '1',
|
|
103
|
-
iv,
|
|
104
|
-
tag
|
|
105
|
-
}, encrypted);
|
|
106
|
-
}
|
|
107
|
-
async decrypt(algo, key, encryptedData) {
|
|
108
|
-
await this.__init();
|
|
109
|
-
const keyHash = crypto_1.default.createHash('sha256').update(key).digest();
|
|
110
|
-
return this._decrypt(algo, keyHash, encryptedData);
|
|
111
|
-
}
|
|
112
|
-
async encryptRSA(payload, publicKey, exp) {
|
|
113
|
-
await this.__init();
|
|
114
|
-
const symmetricKey = crypto_1.default.randomBytes(32);
|
|
115
|
-
const newPayload = { payload: payload, exp: exp };
|
|
116
|
-
const { iv, encrypted, tag } = this._encrypt('aes-256-gcm', symmetricKey, newPayload);
|
|
117
|
-
const encryptedSymmetricKey = crypto_1.default.publicEncrypt({
|
|
118
|
-
key: publicKey,
|
|
119
|
-
padding: crypto_1.default.constants.RSA_PKCS1_OAEP_PADDING,
|
|
120
|
-
oaepHash: 'sha256'
|
|
121
|
-
}, symmetricKey);
|
|
122
|
-
return (0, functions_lib_1.tokenFormatCreate)({
|
|
123
|
-
runtime: 'node',
|
|
124
|
-
algo: 'RSA+A256GCM',
|
|
125
|
-
type: 'asymmetric',
|
|
126
|
-
v: '1',
|
|
127
|
-
iv,
|
|
128
|
-
tag,
|
|
129
|
-
encryptedKey: encryptedSymmetricKey.toString('base64')
|
|
130
|
-
}, encrypted);
|
|
131
|
-
}
|
|
132
|
-
async decryptRSA(privateKey, encryptedKey, encryptedData) {
|
|
133
|
-
await this.__init();
|
|
134
|
-
const decryptedSymmetricKey = crypto_1.default.privateDecrypt({
|
|
135
|
-
key: privateKey,
|
|
136
|
-
padding: crypto_1.default.constants.RSA_PKCS1_OAEP_PADDING,
|
|
137
|
-
oaepHash: 'sha256'
|
|
138
|
-
}, Buffer.from(encryptedKey, 'base64'));
|
|
139
|
-
return this._decrypt('aes-256-gcm', decryptedSymmetricKey, encryptedData);
|
|
140
|
-
}
|
|
141
|
-
async rsaPrivatePublicKeyGeneration() {
|
|
142
|
-
await this.__init();
|
|
143
|
-
return this._rsaPrivatePublicKeyGeneration();
|
|
144
|
-
}
|
|
145
|
-
async rsaPublicKeyGeneration(privateKeyPem) {
|
|
146
|
-
await this.__init();
|
|
147
|
-
return this._rsaPublicKeyGeneration(privateKeyPem);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
exports.NodeCrypto = NodeCrypto;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export declare class RuntimeCrypto {
|
|
2
|
-
private node;
|
|
3
|
-
private web;
|
|
4
|
-
private getModule;
|
|
5
|
-
createToken(runtime: Runtime, algo: string, key: string, payload: any, exp: number): Promise<string>;
|
|
6
|
-
verifyToken<T>(currentRuntime: Runtime, token: string, key: string): Promise<{
|
|
7
|
-
payload: T;
|
|
8
|
-
exp: number;
|
|
9
|
-
}>;
|
|
10
|
-
rsaKeyDrivation(runtime: Runtime, type: 'keyPair'): Promise<GenerateKeyPair>;
|
|
11
|
-
rsaKeyDrivation(runtime: Runtime, type: 'publickey', privateKeyPem: string): Promise<string>;
|
|
12
|
-
}
|
package/dist/runtime/runtime.js
DELETED
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.RuntimeCrypto = void 0;
|
|
37
|
-
const algo_config_1 = require("../config/algo.config");
|
|
38
|
-
const functions_lib_1 = require("../lib/functions.lib");
|
|
39
|
-
class RuntimeCrypto {
|
|
40
|
-
node;
|
|
41
|
-
web;
|
|
42
|
-
async getModule(runtime) {
|
|
43
|
-
if (runtime === 'node' && this.node === undefined) {
|
|
44
|
-
const { NodeCrypto } = await Promise.resolve().then(() => __importStar(require('./node.runtime')));
|
|
45
|
-
this.node = new NodeCrypto();
|
|
46
|
-
}
|
|
47
|
-
else if (runtime === 'web' && this.web === undefined) {
|
|
48
|
-
const { WebCrypto } = await Promise.resolve().then(() => __importStar(require('./web.runtime')));
|
|
49
|
-
this.web = new WebCrypto();
|
|
50
|
-
}
|
|
51
|
-
else if (runtime === 'edge' && this.web === undefined) {
|
|
52
|
-
const { WebCrypto } = await Promise.resolve().then(() => __importStar(require('./web.runtime')));
|
|
53
|
-
this.web = new WebCrypto();
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
async createToken(runtime, algo, key, payload, exp) {
|
|
57
|
-
const algorithms = algo_config_1.SUPPORTED_ALGORITHM[runtime];
|
|
58
|
-
const algoData = algorithms.find(({ name }) => { return name == algo; });
|
|
59
|
-
if (!algoData) {
|
|
60
|
-
throw new Error(`Algorithm ${algo} is not supported for ${runtime}`);
|
|
61
|
-
}
|
|
62
|
-
await this.getModule(runtime);
|
|
63
|
-
if (runtime === 'node') {
|
|
64
|
-
if (algoData.type === 'asymmetric') {
|
|
65
|
-
if (algoData.value !== 'ras+a256gcm') {
|
|
66
|
-
throw new Error(`Algorithm ${algoData.name} is not supported for asymmetric encryption`);
|
|
67
|
-
}
|
|
68
|
-
return await this.node.encryptRSA(payload, key, exp);
|
|
69
|
-
}
|
|
70
|
-
else {
|
|
71
|
-
if (algoData.value !== 'aes-256-gcm') {
|
|
72
|
-
throw new Error(`Algorithm ${algoData.name} is not supported for symmetric encryption`);
|
|
73
|
-
}
|
|
74
|
-
return await this.node.encrypt(algoData.value, key, payload, exp);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
else {
|
|
78
|
-
if (algoData.type === 'asymmetric') {
|
|
79
|
-
if (algoData.value !== 'RSA+AES-GCM') {
|
|
80
|
-
throw new Error(`Algorithm ${algoData.name} is not supported for asymmetric encryption`);
|
|
81
|
-
}
|
|
82
|
-
return await this.web.encryptRSA(payload, key, exp);
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
if (algoData.value !== 'AES-GCM') {
|
|
86
|
-
throw new Error(`Algorithm ${algoData.name} is not supported for symmetric encryption`);
|
|
87
|
-
}
|
|
88
|
-
return await this.web.encrypt(algoData.value, key, payload, exp);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
async verifyToken(currentRuntime, token, key) {
|
|
93
|
-
const { meta, encrypted } = (0, functions_lib_1.tokenFormatVerify)(token);
|
|
94
|
-
const { runtime, algo, type, v, iv, tag, encryptedKey } = meta;
|
|
95
|
-
if (currentRuntime !== runtime) {
|
|
96
|
-
throw new Error('Runtime not matching');
|
|
97
|
-
}
|
|
98
|
-
await this.getModule(runtime);
|
|
99
|
-
if (runtime === 'node') {
|
|
100
|
-
if (type === 'asymmetric') {
|
|
101
|
-
if (algo !== 'RSA+A256GCM') {
|
|
102
|
-
throw new Error(`Algorithm ${algo} is not supported for asymmetric encryption`);
|
|
103
|
-
}
|
|
104
|
-
return await this.node.decryptRSA(key, encryptedKey, { iv, encrypted, tag });
|
|
105
|
-
}
|
|
106
|
-
else {
|
|
107
|
-
if (algo !== 'AES-256-GCM') {
|
|
108
|
-
throw new Error(`Algorithm ${algo} is not supported for symmetric encryption`);
|
|
109
|
-
}
|
|
110
|
-
return await this.node.decrypt("aes-256-gcm", key, { iv, encrypted, tag });
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
if (type === 'asymmetric') {
|
|
115
|
-
if (algo !== 'RSA+AES-GCM') {
|
|
116
|
-
throw new Error(`Algorithm ${algo} is not supported for asymmetric encryption`);
|
|
117
|
-
}
|
|
118
|
-
return await this.web.decryptRSA(key, encryptedKey, { iv, encrypted });
|
|
119
|
-
}
|
|
120
|
-
else {
|
|
121
|
-
if (algo !== 'AES-GCM') {
|
|
122
|
-
throw new Error(`Algorithm ${algo} is not supported for symmetric encryption`);
|
|
123
|
-
}
|
|
124
|
-
return await this.web.decrypt('AES-GCM', key, { iv, encrypted });
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
async rsaKeyDrivation(runtime, type, privateKeyPem) {
|
|
129
|
-
await this.getModule(runtime);
|
|
130
|
-
if (runtime === 'node') {
|
|
131
|
-
if (type === 'keyPair') {
|
|
132
|
-
return await this.node.rsaPrivatePublicKeyGeneration();
|
|
133
|
-
}
|
|
134
|
-
else {
|
|
135
|
-
if (!privateKeyPem)
|
|
136
|
-
throw new Error('privateKeyPem is required');
|
|
137
|
-
return await this.node.rsaPublicKeyGeneration(privateKeyPem);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
else {
|
|
141
|
-
if (type === 'keyPair') {
|
|
142
|
-
return await this.web.rsaPrivatePublicKeyGeneration();
|
|
143
|
-
}
|
|
144
|
-
else {
|
|
145
|
-
if (!privateKeyPem)
|
|
146
|
-
throw new Error('privateKeyPem is required');
|
|
147
|
-
return await this.web.rsaPublicKeyGeneration(privateKeyPem);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
exports.RuntimeCrypto = RuntimeCrypto;
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
export declare class WebCrypto {
|
|
2
|
-
private textEncoder;
|
|
3
|
-
private textDecoder;
|
|
4
|
-
private __importAESKey;
|
|
5
|
-
private __uint8ArrayToBase64;
|
|
6
|
-
private __base64ToUint8Array;
|
|
7
|
-
private __arrayBufferToBase64;
|
|
8
|
-
private __base64ToArrayBuffer;
|
|
9
|
-
private __pemToArrayBuffer;
|
|
10
|
-
private __arrayBufferToPem;
|
|
11
|
-
private __importPublicKey;
|
|
12
|
-
private __importPrivateKey;
|
|
13
|
-
private _encrypt;
|
|
14
|
-
private _decrypt;
|
|
15
|
-
private _rsaPublicKeyGeneration;
|
|
16
|
-
private _rsaPrivatePublicKeyGeneration;
|
|
17
|
-
encrypt(algo: 'AES-GCM', key: string, payload: any, exp: number): Promise<string>;
|
|
18
|
-
decrypt<T>(algo: 'AES-GCM', key: string, encryptedData: {
|
|
19
|
-
iv: string;
|
|
20
|
-
encrypted: string;
|
|
21
|
-
}): Promise<{
|
|
22
|
-
payload: T;
|
|
23
|
-
exp: number;
|
|
24
|
-
}>;
|
|
25
|
-
encryptRSA(payload: any, publicKeyPem: string, exp: number): Promise<string>;
|
|
26
|
-
decryptRSA<T>(privateKeyPem: string, encryptedKey: string, encryptedData: {
|
|
27
|
-
iv: string;
|
|
28
|
-
encrypted: string;
|
|
29
|
-
}): Promise<{
|
|
30
|
-
payload: T;
|
|
31
|
-
exp: number;
|
|
32
|
-
}>;
|
|
33
|
-
rsaPrivatePublicKeyGeneration(): Promise<{
|
|
34
|
-
privateKey: string;
|
|
35
|
-
publicKey: string;
|
|
36
|
-
}>;
|
|
37
|
-
rsaPublicKeyGeneration(privateKeyPem: string): Promise<string>;
|
|
38
|
-
}
|
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.WebCrypto = void 0;
|
|
4
|
-
const functions_lib_1 = require("../lib/functions.lib");
|
|
5
|
-
class WebCrypto {
|
|
6
|
-
textEncoder = new TextEncoder();
|
|
7
|
-
textDecoder = new TextDecoder();
|
|
8
|
-
async __importAESKey(password) {
|
|
9
|
-
const passwordBuffer = this.textEncoder.encode(password);
|
|
10
|
-
const hashBuffer = await crypto.subtle.digest('SHA-256', passwordBuffer);
|
|
11
|
-
return await crypto.subtle.importKey('raw', hashBuffer, { name: 'AES-GCM' }, false, ['encrypt', 'decrypt']);
|
|
12
|
-
}
|
|
13
|
-
__uint8ArrayToBase64(uint8Array) {
|
|
14
|
-
let binary = '';
|
|
15
|
-
const len = uint8Array.byteLength;
|
|
16
|
-
for (let i = 0; i < len; i++) {
|
|
17
|
-
binary += String.fromCharCode(uint8Array[i]);
|
|
18
|
-
}
|
|
19
|
-
return btoa(binary);
|
|
20
|
-
}
|
|
21
|
-
__base64ToUint8Array(base64) {
|
|
22
|
-
const binaryString = atob(base64);
|
|
23
|
-
const len = binaryString.length;
|
|
24
|
-
const bytes = new Uint8Array(len);
|
|
25
|
-
for (let i = 0; i < len; i++) {
|
|
26
|
-
bytes[i] = binaryString.charCodeAt(i);
|
|
27
|
-
}
|
|
28
|
-
return bytes;
|
|
29
|
-
}
|
|
30
|
-
__arrayBufferToBase64(buffer) {
|
|
31
|
-
const bytes = new Uint8Array(buffer);
|
|
32
|
-
let binary = '';
|
|
33
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
34
|
-
binary += String.fromCharCode(bytes[i]);
|
|
35
|
-
}
|
|
36
|
-
return btoa(binary);
|
|
37
|
-
}
|
|
38
|
-
__base64ToArrayBuffer(base64) {
|
|
39
|
-
const binary = atob(base64);
|
|
40
|
-
const bytes = new Uint8Array(binary.length);
|
|
41
|
-
for (let i = 0; i < binary.length; i++) {
|
|
42
|
-
bytes[i] = binary.charCodeAt(i);
|
|
43
|
-
}
|
|
44
|
-
return bytes.buffer;
|
|
45
|
-
}
|
|
46
|
-
__pemToArrayBuffer(pem, type) {
|
|
47
|
-
const pemHeader = `-----BEGIN ${type} KEY-----`;
|
|
48
|
-
const pemFooter = `-----END ${type} KEY-----`;
|
|
49
|
-
const pemContents = pem
|
|
50
|
-
.replace(pemHeader, '')
|
|
51
|
-
.replace(pemFooter, '')
|
|
52
|
-
.replace(/\\n/g, '')
|
|
53
|
-
.replace(/\s/g, '');
|
|
54
|
-
return this.__base64ToArrayBuffer(pemContents);
|
|
55
|
-
}
|
|
56
|
-
__arrayBufferToPem(buffer, type) {
|
|
57
|
-
const base64 = this.__arrayBufferToBase64(buffer);
|
|
58
|
-
const pemContents = base64.match(/.{1,64}/g)?.join('\n') || base64;
|
|
59
|
-
return `-----BEGIN ${type} KEY-----\n${pemContents}\n-----END ${type} KEY-----`;
|
|
60
|
-
}
|
|
61
|
-
async __importPublicKey(publicKeyPem) {
|
|
62
|
-
const keyData = this.__pemToArrayBuffer(publicKeyPem, 'PUBLIC');
|
|
63
|
-
return await crypto.subtle.importKey('spki', keyData, {
|
|
64
|
-
name: 'RSA-OAEP',
|
|
65
|
-
hash: 'SHA-256'
|
|
66
|
-
}, true, ['encrypt']);
|
|
67
|
-
}
|
|
68
|
-
async __importPrivateKey(privateKeyPem) {
|
|
69
|
-
const keyData = this.__pemToArrayBuffer(privateKeyPem.replace(/\\n/g, '\n'), 'PRIVATE');
|
|
70
|
-
return await crypto.subtle.importKey('pkcs8', keyData, {
|
|
71
|
-
name: 'RSA-OAEP',
|
|
72
|
-
hash: 'SHA-256'
|
|
73
|
-
}, true, ['decrypt']);
|
|
74
|
-
}
|
|
75
|
-
async _encrypt(algo, key, payload) {
|
|
76
|
-
const iv = crypto.getRandomValues(new Uint8Array(12));
|
|
77
|
-
const data = this.textEncoder.encode(JSON.stringify(payload));
|
|
78
|
-
const encryptedBuffer = await crypto.subtle.encrypt({
|
|
79
|
-
name: algo,
|
|
80
|
-
iv: iv,
|
|
81
|
-
tagLength: 128
|
|
82
|
-
}, key, data);
|
|
83
|
-
const encryptedBytes = new Uint8Array(encryptedBuffer);
|
|
84
|
-
return {
|
|
85
|
-
iv: this.__uint8ArrayToBase64(iv),
|
|
86
|
-
encrypted: this.__uint8ArrayToBase64(encryptedBytes),
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
async _decrypt(algo, key, encryptedData) {
|
|
90
|
-
const { iv, encrypted } = encryptedData;
|
|
91
|
-
const ivBuffer = this.__base64ToUint8Array(iv);
|
|
92
|
-
const encryptedBuffer = this.__base64ToUint8Array(encrypted);
|
|
93
|
-
const decryptedBuffer = await crypto.subtle.decrypt({
|
|
94
|
-
name: algo,
|
|
95
|
-
iv: ivBuffer,
|
|
96
|
-
tagLength: 128
|
|
97
|
-
}, key, encryptedBuffer);
|
|
98
|
-
return JSON.parse(this.textDecoder.decode(decryptedBuffer));
|
|
99
|
-
}
|
|
100
|
-
async _rsaPublicKeyGeneration(privateKeyPem) {
|
|
101
|
-
const privateKey = await this.__importPrivateKey(privateKeyPem);
|
|
102
|
-
const jwk = await crypto.subtle.exportKey('jwk', privateKey);
|
|
103
|
-
delete jwk.d;
|
|
104
|
-
delete jwk.dp;
|
|
105
|
-
delete jwk.dq;
|
|
106
|
-
delete jwk.q;
|
|
107
|
-
delete jwk.qi;
|
|
108
|
-
jwk.key_ops = ['encrypt'];
|
|
109
|
-
const publicKey = await crypto.subtle.importKey('jwk', jwk, {
|
|
110
|
-
name: 'RSA-OAEP',
|
|
111
|
-
hash: 'SHA-256'
|
|
112
|
-
}, true, ['encrypt']);
|
|
113
|
-
const exportedPublicKey = await crypto.subtle.exportKey('spki', publicKey);
|
|
114
|
-
return this.__arrayBufferToPem(exportedPublicKey, 'PUBLIC');
|
|
115
|
-
}
|
|
116
|
-
async _rsaPrivatePublicKeyGeneration() {
|
|
117
|
-
const keyPair = await crypto.subtle.generateKey({
|
|
118
|
-
name: 'RSA-OAEP',
|
|
119
|
-
modulusLength: 2048,
|
|
120
|
-
publicExponent: new Uint8Array([1, 0, 1]),
|
|
121
|
-
hash: 'SHA-256'
|
|
122
|
-
}, true, ['encrypt', 'decrypt']);
|
|
123
|
-
const privateKeyBuffer = await crypto.subtle.exportKey('pkcs8', keyPair.privateKey);
|
|
124
|
-
const publicKeyBuffer = await crypto.subtle.exportKey('spki', keyPair.publicKey);
|
|
125
|
-
return {
|
|
126
|
-
privateKey: this.__arrayBufferToPem(privateKeyBuffer, 'PRIVATE'),
|
|
127
|
-
publicKey: this.__arrayBufferToPem(publicKeyBuffer, 'PUBLIC')
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
async encrypt(algo, key, payload, exp) {
|
|
131
|
-
const cryptoKey = await this.__importAESKey(key);
|
|
132
|
-
const newPayload = { payload: payload, exp: exp };
|
|
133
|
-
const { iv, encrypted } = await this._encrypt(algo, cryptoKey, newPayload);
|
|
134
|
-
return (0, functions_lib_1.tokenFormatCreate)({
|
|
135
|
-
runtime: 'web',
|
|
136
|
-
algo: 'AES-GCM',
|
|
137
|
-
type: 'symmetric',
|
|
138
|
-
v: '1',
|
|
139
|
-
iv,
|
|
140
|
-
}, encrypted);
|
|
141
|
-
}
|
|
142
|
-
async decrypt(algo, key, encryptedData) {
|
|
143
|
-
const cryptoKey = await this.__importAESKey(key);
|
|
144
|
-
return await this._decrypt(algo, cryptoKey, encryptedData);
|
|
145
|
-
}
|
|
146
|
-
async encryptRSA(payload, publicKeyPem, exp) {
|
|
147
|
-
const symmetricKey = await crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, true, ['encrypt', 'decrypt']);
|
|
148
|
-
const newPayload = { payload: payload, exp: exp };
|
|
149
|
-
const { iv, encrypted } = await this._encrypt('AES-GCM', symmetricKey, newPayload);
|
|
150
|
-
const symmetricKeyBuffer = await crypto.subtle.exportKey('raw', symmetricKey);
|
|
151
|
-
const publicKey = await this.__importPublicKey(publicKeyPem);
|
|
152
|
-
const encryptedSymmetricKey = await crypto.subtle.encrypt({
|
|
153
|
-
name: 'RSA-OAEP'
|
|
154
|
-
}, publicKey, symmetricKeyBuffer);
|
|
155
|
-
return (0, functions_lib_1.tokenFormatCreate)({
|
|
156
|
-
runtime: 'web',
|
|
157
|
-
algo: 'RSA+AES-GCM',
|
|
158
|
-
type: 'asymmetric',
|
|
159
|
-
v: '1',
|
|
160
|
-
iv,
|
|
161
|
-
encryptedKey: this.__arrayBufferToBase64(encryptedSymmetricKey)
|
|
162
|
-
}, encrypted);
|
|
163
|
-
}
|
|
164
|
-
async decryptRSA(privateKeyPem, encryptedKey, encryptedData) {
|
|
165
|
-
const privateKey = await this.__importPrivateKey(privateKeyPem);
|
|
166
|
-
const encryptedKeyBuffer = this.__base64ToArrayBuffer(encryptedKey);
|
|
167
|
-
const decryptedSymmetricKeyBuffer = await crypto.subtle.decrypt({
|
|
168
|
-
name: 'RSA-OAEP'
|
|
169
|
-
}, privateKey, encryptedKeyBuffer);
|
|
170
|
-
const symmetricKey = await crypto.subtle.importKey('raw', decryptedSymmetricKeyBuffer, { name: 'AES-GCM' }, false, ['decrypt']);
|
|
171
|
-
return await this._decrypt('AES-GCM', symmetricKey, encryptedData);
|
|
172
|
-
}
|
|
173
|
-
async rsaPrivatePublicKeyGeneration() {
|
|
174
|
-
return await this._rsaPrivatePublicKeyGeneration();
|
|
175
|
-
}
|
|
176
|
-
async rsaPublicKeyGeneration(privateKeyPem) {
|
|
177
|
-
return await this._rsaPublicKeyGeneration(privateKeyPem);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
exports.WebCrypto = WebCrypto;
|