@naturalcycles/nodejs-lib 12.77.1 → 12.78.0
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/security/crypto.util.d.ts +7 -7
- package/dist/security/hash.util.d.ts +7 -12
- package/dist/security/hash.util.js +1 -7
- package/dist/security/secret.util.d.ts +3 -3
- package/package.json +1 -1
- package/src/security/crypto.util.ts +7 -7
- package/src/security/hash.util.ts +8 -13
- package/src/security/secret.util.ts +3 -3
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { StringMap } from '@naturalcycles/js-lib';
|
|
2
|
+
import { Base64String, StringMap } from '@naturalcycles/js-lib';
|
|
3
3
|
/**
|
|
4
4
|
* Using aes-256-cbc
|
|
5
5
|
*/
|
|
6
|
-
export declare function encryptRandomIVBuffer(input: Buffer, secretKeyBase64:
|
|
6
|
+
export declare function encryptRandomIVBuffer(input: Buffer, secretKeyBase64: Base64String): Buffer;
|
|
7
7
|
/**
|
|
8
8
|
* Using aes-256-cbc
|
|
9
9
|
*/
|
|
10
|
-
export declare function decryptRandomIVBuffer(input: Buffer, secretKeyBase64:
|
|
10
|
+
export declare function decryptRandomIVBuffer(input: Buffer, secretKeyBase64: Base64String): Buffer;
|
|
11
11
|
/**
|
|
12
12
|
* Decrypts all object values.
|
|
13
13
|
* Returns object with decrypted values.
|
|
14
14
|
*/
|
|
15
|
-
export declare function decryptObject(obj: StringMap
|
|
16
|
-
export declare function encryptObject(obj: StringMap, secretKey: string): StringMap
|
|
15
|
+
export declare function decryptObject(obj: StringMap<Base64String>, secretKey: string): StringMap;
|
|
16
|
+
export declare function encryptObject(obj: StringMap, secretKey: string): StringMap<Base64String>;
|
|
17
17
|
/**
|
|
18
18
|
* Using aes-256-cbc
|
|
19
19
|
*/
|
|
20
|
-
export declare function decryptString(str:
|
|
20
|
+
export declare function decryptString(str: Base64String, secretKey: string): string;
|
|
21
21
|
/**
|
|
22
22
|
* Using aes-256-cbc
|
|
23
23
|
*/
|
|
24
|
-
export declare function encryptString(str: string, secretKey: string):
|
|
24
|
+
export declare function encryptString(str: string, secretKey: string): Base64String;
|
|
@@ -1,20 +1,15 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
+
import { Base64String } from '@naturalcycles/js-lib';
|
|
2
3
|
export declare function md5(s: string | Buffer): string;
|
|
3
|
-
export declare function md5AsBase64(s: string | Buffer):
|
|
4
|
+
export declare function md5AsBase64(s: string | Buffer): Base64String;
|
|
4
5
|
export declare function md5AsBuffer(s: string | Buffer): Buffer;
|
|
5
6
|
export declare function sha256(s: string | Buffer): string;
|
|
6
|
-
export declare function sha256AsBase64(s: string | Buffer):
|
|
7
|
+
export declare function sha256AsBase64(s: string | Buffer): Base64String;
|
|
7
8
|
export declare function sha256AsBuffer(s: string | Buffer): Buffer;
|
|
8
9
|
export declare function hash(s: string | Buffer, algorithm: string): string;
|
|
9
10
|
export declare function hashAsBuffer(s: string | Buffer, algorithm: string): Buffer;
|
|
10
|
-
export declare function base64(s: string | Buffer):
|
|
11
|
-
export declare function base64ToString(strBase64:
|
|
11
|
+
export declare function base64(s: string | Buffer): Base64String;
|
|
12
|
+
export declare function base64ToString(strBase64: Base64String): string;
|
|
12
13
|
export declare function base64ToBuffer(strBase64: string): Buffer;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
*/
|
|
16
|
-
export declare function stringToBase64(s: string): string;
|
|
17
|
-
/**
|
|
18
|
-
* @deprecated use `base64`
|
|
19
|
-
*/
|
|
20
|
-
export declare function bufferToBase64(b: Buffer): string;
|
|
14
|
+
export declare function stringToBase64(s: string): Base64String;
|
|
15
|
+
export declare function bufferToBase64(b: Buffer): Base64String;
|
|
@@ -35,7 +35,7 @@ function hashAsBuffer(s, algorithm) {
|
|
|
35
35
|
}
|
|
36
36
|
exports.hashAsBuffer = hashAsBuffer;
|
|
37
37
|
function base64(s) {
|
|
38
|
-
return (
|
|
38
|
+
return (typeof s === 'string' ? Buffer.from(s) : s).toString('base64');
|
|
39
39
|
}
|
|
40
40
|
exports.base64 = base64;
|
|
41
41
|
function base64ToString(strBase64) {
|
|
@@ -46,16 +46,10 @@ function base64ToBuffer(strBase64) {
|
|
|
46
46
|
return Buffer.from(strBase64, 'base64');
|
|
47
47
|
}
|
|
48
48
|
exports.base64ToBuffer = base64ToBuffer;
|
|
49
|
-
/**
|
|
50
|
-
* @deprecated use `base64`
|
|
51
|
-
*/
|
|
52
49
|
function stringToBase64(s) {
|
|
53
50
|
return Buffer.from(s, 'utf8').toString('base64');
|
|
54
51
|
}
|
|
55
52
|
exports.stringToBase64 = stringToBase64;
|
|
56
|
-
/**
|
|
57
|
-
* @deprecated use `base64`
|
|
58
|
-
*/
|
|
59
53
|
function bufferToBase64(b) {
|
|
60
54
|
return b.toString('base64');
|
|
61
55
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { StringMap } from '@naturalcycles/js-lib';
|
|
1
|
+
import { Base64String, StringMap } from '@naturalcycles/js-lib';
|
|
2
2
|
/**
|
|
3
3
|
* Loads plaintext secrets from process.env, removes them, stores locally.
|
|
4
4
|
* Make sure to call this function early on server startup, so secrets are removed from process.env
|
|
@@ -18,12 +18,12 @@ export declare function removeSecretsFromEnv(): void;
|
|
|
18
18
|
* Whole file is encrypted.
|
|
19
19
|
* For "json-values encrypted" style - use `loadSecretsFromEncryptedJsonFileValues`
|
|
20
20
|
*/
|
|
21
|
-
export declare function loadSecretsFromEncryptedJsonFile(filePath: string, secretEncryptionKey?:
|
|
21
|
+
export declare function loadSecretsFromEncryptedJsonFile(filePath: string, secretEncryptionKey?: Base64String): void;
|
|
22
22
|
/**
|
|
23
23
|
* Whole file is NOT encrypted, but instead individual json values ARE encrypted..
|
|
24
24
|
* For whole-file encryption - use `loadSecretsFromEncryptedJsonFile`
|
|
25
25
|
*/
|
|
26
|
-
export declare function loadSecretsFromEncryptedJsonFileValues(filePath: string, secretEncryptionKey?:
|
|
26
|
+
export declare function loadSecretsFromEncryptedJsonFileValues(filePath: string, secretEncryptionKey?: Base64String): void;
|
|
27
27
|
/**
|
|
28
28
|
* json secrets are always base64'd
|
|
29
29
|
*/
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as crypto from 'node:crypto'
|
|
2
|
-
import { _stringMapEntries, StringMap } from '@naturalcycles/js-lib'
|
|
2
|
+
import { _stringMapEntries, Base64String, StringMap } from '@naturalcycles/js-lib'
|
|
3
3
|
import { md5 } from './hash.util'
|
|
4
4
|
|
|
5
5
|
const algorithm = 'aes-256-cbc'
|
|
@@ -7,7 +7,7 @@ const algorithm = 'aes-256-cbc'
|
|
|
7
7
|
/**
|
|
8
8
|
* Using aes-256-cbc
|
|
9
9
|
*/
|
|
10
|
-
export function encryptRandomIVBuffer(input: Buffer, secretKeyBase64:
|
|
10
|
+
export function encryptRandomIVBuffer(input: Buffer, secretKeyBase64: Base64String): Buffer {
|
|
11
11
|
// md5 to match aes-256 key length of 32 bytes
|
|
12
12
|
const key = md5(Buffer.from(secretKeyBase64, 'base64'))
|
|
13
13
|
|
|
@@ -21,7 +21,7 @@ export function encryptRandomIVBuffer(input: Buffer, secretKeyBase64: string): B
|
|
|
21
21
|
/**
|
|
22
22
|
* Using aes-256-cbc
|
|
23
23
|
*/
|
|
24
|
-
export function decryptRandomIVBuffer(input: Buffer, secretKeyBase64:
|
|
24
|
+
export function decryptRandomIVBuffer(input: Buffer, secretKeyBase64: Base64String): Buffer {
|
|
25
25
|
// md5 to match aes-256 key length of 32 bytes
|
|
26
26
|
const key = md5(Buffer.from(secretKeyBase64, 'base64'))
|
|
27
27
|
|
|
@@ -38,7 +38,7 @@ export function decryptRandomIVBuffer(input: Buffer, secretKeyBase64: string): B
|
|
|
38
38
|
* Decrypts all object values.
|
|
39
39
|
* Returns object with decrypted values.
|
|
40
40
|
*/
|
|
41
|
-
export function decryptObject(obj: StringMap
|
|
41
|
+
export function decryptObject(obj: StringMap<Base64String>, secretKey: string): StringMap {
|
|
42
42
|
const { key, iv } = getCryptoParams(secretKey)
|
|
43
43
|
|
|
44
44
|
const r: StringMap = {}
|
|
@@ -49,7 +49,7 @@ export function decryptObject(obj: StringMap, secretKey: string): StringMap {
|
|
|
49
49
|
return r
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
export function encryptObject(obj: StringMap, secretKey: string): StringMap {
|
|
52
|
+
export function encryptObject(obj: StringMap, secretKey: string): StringMap<Base64String> {
|
|
53
53
|
const { key, iv } = getCryptoParams(secretKey)
|
|
54
54
|
|
|
55
55
|
const r: StringMap = {}
|
|
@@ -63,7 +63,7 @@ export function encryptObject(obj: StringMap, secretKey: string): StringMap {
|
|
|
63
63
|
/**
|
|
64
64
|
* Using aes-256-cbc
|
|
65
65
|
*/
|
|
66
|
-
export function decryptString(str:
|
|
66
|
+
export function decryptString(str: Base64String, secretKey: string): string {
|
|
67
67
|
const { key, iv } = getCryptoParams(secretKey)
|
|
68
68
|
const decipher = crypto.createDecipheriv(algorithm, key, iv)
|
|
69
69
|
return decipher.update(str, 'base64', 'utf8') + decipher.final('utf8')
|
|
@@ -72,7 +72,7 @@ export function decryptString(str: string, secretKey: string): string {
|
|
|
72
72
|
/**
|
|
73
73
|
* Using aes-256-cbc
|
|
74
74
|
*/
|
|
75
|
-
export function encryptString(str: string, secretKey: string):
|
|
75
|
+
export function encryptString(str: string, secretKey: string): Base64String {
|
|
76
76
|
const { key, iv } = getCryptoParams(secretKey)
|
|
77
77
|
const cipher = crypto.createCipheriv(algorithm, key, iv)
|
|
78
78
|
return cipher.update(str, 'utf8', 'base64') + cipher.final('base64')
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import * as crypto from 'node:crypto'
|
|
2
|
+
import { Base64String } from '@naturalcycles/js-lib'
|
|
2
3
|
|
|
3
4
|
export function md5(s: string | Buffer): string {
|
|
4
5
|
return hash(s, 'md5')
|
|
5
6
|
}
|
|
6
7
|
|
|
7
|
-
export function md5AsBase64(s: string | Buffer):
|
|
8
|
+
export function md5AsBase64(s: string | Buffer): Base64String {
|
|
8
9
|
return hashAsBuffer(s, 'md5').toString('base64')
|
|
9
10
|
}
|
|
10
11
|
|
|
@@ -16,7 +17,7 @@ export function sha256(s: string | Buffer): string {
|
|
|
16
17
|
return hash(s, 'sha256')
|
|
17
18
|
}
|
|
18
19
|
|
|
19
|
-
export function sha256AsBase64(s: string | Buffer):
|
|
20
|
+
export function sha256AsBase64(s: string | Buffer): Base64String {
|
|
20
21
|
return hashAsBuffer(s, 'sha256').toString('base64')
|
|
21
22
|
}
|
|
22
23
|
|
|
@@ -32,11 +33,11 @@ export function hashAsBuffer(s: string | Buffer, algorithm: string): Buffer {
|
|
|
32
33
|
return crypto.createHash(algorithm).update(s).digest()
|
|
33
34
|
}
|
|
34
35
|
|
|
35
|
-
export function base64(s: string | Buffer):
|
|
36
|
-
return (
|
|
36
|
+
export function base64(s: string | Buffer): Base64String {
|
|
37
|
+
return (typeof s === 'string' ? Buffer.from(s) : s).toString('base64')
|
|
37
38
|
}
|
|
38
39
|
|
|
39
|
-
export function base64ToString(strBase64:
|
|
40
|
+
export function base64ToString(strBase64: Base64String): string {
|
|
40
41
|
return Buffer.from(strBase64, 'base64').toString('utf8')
|
|
41
42
|
}
|
|
42
43
|
|
|
@@ -44,16 +45,10 @@ export function base64ToBuffer(strBase64: string): Buffer {
|
|
|
44
45
|
return Buffer.from(strBase64, 'base64')
|
|
45
46
|
}
|
|
46
47
|
|
|
47
|
-
|
|
48
|
-
* @deprecated use `base64`
|
|
49
|
-
*/
|
|
50
|
-
export function stringToBase64(s: string): string {
|
|
48
|
+
export function stringToBase64(s: string): Base64String {
|
|
51
49
|
return Buffer.from(s, 'utf8').toString('base64')
|
|
52
50
|
}
|
|
53
51
|
|
|
54
|
-
|
|
55
|
-
* @deprecated use `base64`
|
|
56
|
-
*/
|
|
57
|
-
export function bufferToBase64(b: Buffer): string {
|
|
52
|
+
export function bufferToBase64(b: Buffer): Base64String {
|
|
58
53
|
return b.toString('base64')
|
|
59
54
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as fs from 'node:fs'
|
|
2
|
-
import { _assert, StringMap } from '@naturalcycles/js-lib'
|
|
2
|
+
import { _assert, Base64String, StringMap } from '@naturalcycles/js-lib'
|
|
3
3
|
import { base64ToString } from '..'
|
|
4
4
|
import { decryptObject, decryptRandomIVBuffer } from './crypto.util'
|
|
5
5
|
|
|
@@ -53,7 +53,7 @@ export function removeSecretsFromEnv(): void {
|
|
|
53
53
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
54
54
|
export function loadSecretsFromEncryptedJsonFile(
|
|
55
55
|
filePath: string,
|
|
56
|
-
secretEncryptionKey?:
|
|
56
|
+
secretEncryptionKey?: Base64String,
|
|
57
57
|
): void {
|
|
58
58
|
_assert(
|
|
59
59
|
fs.existsSync(filePath),
|
|
@@ -86,7 +86,7 @@ export function loadSecretsFromEncryptedJsonFile(
|
|
|
86
86
|
*/
|
|
87
87
|
export function loadSecretsFromEncryptedJsonFileValues(
|
|
88
88
|
filePath: string,
|
|
89
|
-
secretEncryptionKey?:
|
|
89
|
+
secretEncryptionKey?: Base64String,
|
|
90
90
|
): void {
|
|
91
91
|
_assert(
|
|
92
92
|
fs.existsSync(filePath),
|