@oino-ts/hashid 0.1.0 → 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.
Files changed (2) hide show
  1. package/package.json +3 -3
  2. package/src/OINOHashid.ts +13 -13
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oino-ts/hashid",
3
- "version": "0.1.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,11 +18,11 @@
18
18
  "types": "./dist/types/index.d.ts",
19
19
  "dependencies": {
20
20
  "@types/node": "^20.12.7",
21
- "@oino-ts/common": "0.1.0",
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.0"
25
+ "@oino-ts/types": "0.1.1"
26
26
  },
27
27
  "files": [
28
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
  }