@oino-ts/hashid 0.0.18 → 0.1.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/package.json +3 -2
- package/src/OINOHashid.ts +13 -13
- package/dist/types/OINOHashid.d.ts +0 -40
- package/dist/types/index.d.ts +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oino-ts/hashid",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "OINO TS package for hashid's.",
|
|
5
5
|
"author": "Matias Kiviniemi (pragmatta)",
|
|
6
6
|
"license": "MPL-2.0",
|
|
@@ -18,10 +18,11 @@
|
|
|
18
18
|
"types": "./dist/types/index.d.ts",
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@types/node": "^20.12.7",
|
|
21
|
-
"@oino-ts/
|
|
21
|
+
"@oino-ts/common": "0.1.1",
|
|
22
22
|
"base-x": "5.0.0"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
|
+
"@oino-ts/types": "0.1.1"
|
|
25
26
|
},
|
|
26
27
|
"files": [
|
|
27
28
|
"src/*.ts",
|
package/src/OINOHashid.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import { createCipheriv, createDecipheriv, createHmac, randomFillSync } from 'node:crypto';
|
|
7
|
+
import { BinaryLike, createCipheriv, createDecipheriv, createHmac, randomFillSync } from 'node:crypto';
|
|
8
8
|
import basex from 'base-x'
|
|
9
9
|
|
|
10
10
|
const HASHID_MIN_LENGTH:number = 12
|
|
@@ -63,27 +63,27 @@ export class OINOHashid {
|
|
|
63
63
|
// if seed was given use it for pseudorandom chars, otherwise generate them
|
|
64
64
|
let random_chars:string = ""
|
|
65
65
|
if (this._staticIds) {
|
|
66
|
-
const hmac_seed = createHmac('sha1', this._key)
|
|
66
|
+
const hmac_seed = createHmac('sha1', this._key as BinaryLike)
|
|
67
67
|
hmac_seed.update(this._domainId + " " + cellSeed)
|
|
68
|
-
random_chars = hashidEncoder.encode(hmac_seed.digest()) // hmac_seed.digest('base64url')
|
|
68
|
+
random_chars = hashidEncoder.encode(hmac_seed.digest() as Uint8Array) // hmac_seed.digest('base64url')
|
|
69
69
|
|
|
70
70
|
} else {
|
|
71
|
-
randomFillSync(this._iv, 0, 16)
|
|
72
|
-
random_chars = hashidEncoder.encode(this._iv) // this._iv.toString('base64url')
|
|
71
|
+
randomFillSync(this._iv as Uint8Array, 0, 16)
|
|
72
|
+
random_chars = hashidEncoder.encode(this._iv as Uint8Array) // this._iv.toString('base64url')
|
|
73
73
|
}
|
|
74
|
-
const hmac = createHmac('sha1', this._key)
|
|
74
|
+
const hmac = createHmac('sha1', this._key as Uint8Array)
|
|
75
75
|
let iv_seed:string = random_chars.substring(0, this._minLength)
|
|
76
76
|
hmac.update(this._domainId + " " + iv_seed)
|
|
77
77
|
const iv_data:Buffer = hmac.digest()
|
|
78
|
-
iv_data.copy(this._iv, 0, 0, 16)
|
|
78
|
+
iv_data.copy(this._iv as Uint8Array, 0, 0, 16)
|
|
79
79
|
|
|
80
80
|
let plaintext = id.toString()
|
|
81
81
|
if (plaintext.length < this._minLength) {
|
|
82
82
|
plaintext += " " + random_chars.substring(random_chars.length - (this._minLength - plaintext.length - 1))
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
const cipher = createCipheriv('aes-128-gcm', this._key, this._iv)
|
|
86
|
-
const cryptotext = hashidEncoder.encode(cipher.update(plaintext, 'utf8')) + hashidEncoder.encode(cipher.final())
|
|
85
|
+
const cipher = createCipheriv('aes-128-gcm', this._key as Uint8Array, this._iv as Uint8Array)
|
|
86
|
+
const cryptotext = hashidEncoder.encode(cipher.update(plaintext, 'utf8') as Uint8Array) + hashidEncoder.encode(cipher.final() as Uint8Array)
|
|
87
87
|
return iv_seed + cryptotext
|
|
88
88
|
}
|
|
89
89
|
|
|
@@ -95,16 +95,16 @@ export class OINOHashid {
|
|
|
95
95
|
*/
|
|
96
96
|
decode(hashid:string):string {
|
|
97
97
|
// reproduce nonce from seed
|
|
98
|
-
const hmac = createHmac('sha1', this._key)
|
|
98
|
+
const hmac = createHmac('sha1', this._key as Uint8Array)
|
|
99
99
|
const iv_seed = hashid.substring(0, this._minLength)
|
|
100
100
|
hmac.update(this._domainId + " " + iv_seed)
|
|
101
101
|
const hash:Buffer = hmac.digest()
|
|
102
|
-
hash.copy(this._iv, 0, 0, 16)
|
|
102
|
+
hash.copy(this._iv as Uint8Array, 0, 0, 16)
|
|
103
103
|
|
|
104
104
|
const cryptotext:string = hashid.substring(this._minLength)
|
|
105
105
|
const cryptobytes:Buffer = Buffer.from(hashidEncoder.decode(cryptotext))
|
|
106
|
-
const decipher = createDecipheriv('aes-128-gcm', this._key, this._iv)
|
|
107
|
-
const plaintext = decipher.update(cryptobytes, undefined, 'utf8') //, cryptotext, 'base64url', 'utf8')
|
|
106
|
+
const decipher = createDecipheriv('aes-128-gcm', this._key as Uint8Array, this._iv as Uint8Array)
|
|
107
|
+
const plaintext = decipher.update(cryptobytes as Uint8Array, undefined, 'utf8') //, cryptotext, 'base64url', 'utf8')
|
|
108
108
|
|
|
109
109
|
return plaintext.split(" ")[0]
|
|
110
110
|
}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Hashid implementation for OINO API:s for the purpose of making it infeasible to scan
|
|
3
|
-
* through numeric autoinc keys. It's not a solution to keeping the id secret in insecure
|
|
4
|
-
* channels, just making it hard enough to not iterate through the entire key space. Half
|
|
5
|
-
* of the the hashid length is nonce and half cryptotext, i.e. 16 char hashid 8 chars of
|
|
6
|
-
* base64 encoded nonce ~ 6 bytes or 48 bits of entropy.
|
|
7
|
-
*
|
|
8
|
-
*/
|
|
9
|
-
export declare class OINOHashid {
|
|
10
|
-
private _key;
|
|
11
|
-
private _iv;
|
|
12
|
-
private _domainId;
|
|
13
|
-
private _minLength;
|
|
14
|
-
private _staticIds;
|
|
15
|
-
/**
|
|
16
|
-
* Hashid constructor
|
|
17
|
-
*
|
|
18
|
-
* @param key AES128 key (32 char hex-string)
|
|
19
|
-
* @param domainId a sufficiently unique domain ID in which row-Id's are unique
|
|
20
|
-
* @param minLength minimum length of nonce and crypto
|
|
21
|
-
* @param staticIds whether hash values should remain static per row or random values
|
|
22
|
-
*
|
|
23
|
-
*/
|
|
24
|
-
constructor(key: string, domainId: string, minLength?: number, staticIds?: boolean);
|
|
25
|
-
/**
|
|
26
|
-
* Encode given id value as a hashid either using random data or given seed value for nonce.
|
|
27
|
-
*
|
|
28
|
-
* @param id numeric value
|
|
29
|
-
* @param cellSeed a sufficiently unique seed for the current cell to keep hashids unique but persistent (e.g. fieldname + primarykey values)
|
|
30
|
-
*
|
|
31
|
-
*/
|
|
32
|
-
encode(id: string, cellSeed?: string): string;
|
|
33
|
-
/**
|
|
34
|
-
* Decode given hashid.
|
|
35
|
-
*
|
|
36
|
-
* @param hashid value
|
|
37
|
-
*
|
|
38
|
-
*/
|
|
39
|
-
decode(hashid: string): string;
|
|
40
|
-
}
|
package/dist/types/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { OINOHashid } from "./OINOHashid.js";
|