@oino-ts/hashid 0.0.13 → 0.0.15

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.
@@ -25,17 +25,17 @@ class OINOHashid {
25
25
  _iv;
26
26
  _domainId;
27
27
  _minLength;
28
- _randomIds;
28
+ _staticIds;
29
29
  /**
30
30
  * Hashid constructor
31
31
  *
32
32
  * @param key AES128 key (32 char hex-string)
33
33
  * @param domainId a sufficiently unique domain ID in which row-Id's are unique
34
34
  * @param minLength minimum length of nonce and crypto
35
- * @param randomIds whether hash values should remain static per row or random values
35
+ * @param staticIds whether hash values should remain static per row or random values
36
36
  *
37
37
  */
38
- constructor(key, domainId, minLength = HASHID_MIN_LENGTH, randomIds = false) {
38
+ constructor(key, domainId, minLength = HASHID_MIN_LENGTH, staticIds = false) {
39
39
  this._domainId = domainId;
40
40
  if ((minLength < HASHID_MIN_LENGTH) || (minLength > HASHID_MAX_LENGTH)) {
41
41
  throw Error("OINOHashid minLength needs to be between " + HASHID_MIN_LENGTH + " and " + HASHID_MAX_LENGTH + "!");
@@ -44,7 +44,7 @@ class OINOHashid {
44
44
  if (key.length != 32) {
45
45
  throw Error("OINOHashid key needs to be a 32 character hex-string!");
46
46
  }
47
- this._randomIds = randomIds;
47
+ this._staticIds = staticIds;
48
48
  this._key = Buffer.from(key, 'hex');
49
49
  this._iv = new Buffer(16);
50
50
  }
@@ -58,15 +58,15 @@ class OINOHashid {
58
58
  encode(id, cellSeed = "") {
59
59
  // if seed was given use it for pseudorandom chars, otherwise generate them
60
60
  let random_chars = "";
61
- if (this._randomIds) {
62
- (0, node_crypto_1.randomFillSync)(this._iv, 0, 16);
63
- random_chars = hashidEncoder.encode(this._iv); // this._iv.toString('base64url')
64
- }
65
- else {
61
+ if (this._staticIds) {
66
62
  const hmac_seed = (0, node_crypto_1.createHmac)('sha1', this._key);
67
63
  hmac_seed.update(this._domainId + " " + cellSeed);
68
64
  random_chars = hashidEncoder.encode(hmac_seed.digest()); // hmac_seed.digest('base64url')
69
65
  }
66
+ else {
67
+ (0, node_crypto_1.randomFillSync)(this._iv, 0, 16);
68
+ random_chars = hashidEncoder.encode(this._iv); // this._iv.toString('base64url')
69
+ }
70
70
  const hmac = (0, node_crypto_1.createHmac)('sha1', this._key);
71
71
  let iv_seed = random_chars.substring(0, this._minLength);
72
72
  hmac.update(this._domainId + " " + iv_seed);
@@ -22,17 +22,17 @@ export class OINOHashid {
22
22
  _iv;
23
23
  _domainId;
24
24
  _minLength;
25
- _randomIds;
25
+ _staticIds;
26
26
  /**
27
27
  * Hashid constructor
28
28
  *
29
29
  * @param key AES128 key (32 char hex-string)
30
30
  * @param domainId a sufficiently unique domain ID in which row-Id's are unique
31
31
  * @param minLength minimum length of nonce and crypto
32
- * @param randomIds whether hash values should remain static per row or random values
32
+ * @param staticIds whether hash values should remain static per row or random values
33
33
  *
34
34
  */
35
- constructor(key, domainId, minLength = HASHID_MIN_LENGTH, randomIds = false) {
35
+ constructor(key, domainId, minLength = HASHID_MIN_LENGTH, staticIds = false) {
36
36
  this._domainId = domainId;
37
37
  if ((minLength < HASHID_MIN_LENGTH) || (minLength > HASHID_MAX_LENGTH)) {
38
38
  throw Error("OINOHashid minLength needs to be between " + HASHID_MIN_LENGTH + " and " + HASHID_MAX_LENGTH + "!");
@@ -41,7 +41,7 @@ export class OINOHashid {
41
41
  if (key.length != 32) {
42
42
  throw Error("OINOHashid key needs to be a 32 character hex-string!");
43
43
  }
44
- this._randomIds = randomIds;
44
+ this._staticIds = staticIds;
45
45
  this._key = Buffer.from(key, 'hex');
46
46
  this._iv = new Buffer(16);
47
47
  }
@@ -55,15 +55,15 @@ export class OINOHashid {
55
55
  encode(id, cellSeed = "") {
56
56
  // if seed was given use it for pseudorandom chars, otherwise generate them
57
57
  let random_chars = "";
58
- if (this._randomIds) {
59
- randomFillSync(this._iv, 0, 16);
60
- random_chars = hashidEncoder.encode(this._iv); // this._iv.toString('base64url')
61
- }
62
- else {
58
+ if (this._staticIds) {
63
59
  const hmac_seed = createHmac('sha1', this._key);
64
60
  hmac_seed.update(this._domainId + " " + cellSeed);
65
61
  random_chars = hashidEncoder.encode(hmac_seed.digest()); // hmac_seed.digest('base64url')
66
62
  }
63
+ else {
64
+ randomFillSync(this._iv, 0, 16);
65
+ random_chars = hashidEncoder.encode(this._iv); // this._iv.toString('base64url')
66
+ }
67
67
  const hmac = createHmac('sha1', this._key);
68
68
  let iv_seed = random_chars.substring(0, this._minLength);
69
69
  hmac.update(this._domainId + " " + iv_seed);
@@ -11,17 +11,17 @@ export declare class OINOHashid {
11
11
  private _iv;
12
12
  private _domainId;
13
13
  private _minLength;
14
- private _randomIds;
14
+ private _staticIds;
15
15
  /**
16
16
  * Hashid constructor
17
17
  *
18
18
  * @param key AES128 key (32 char hex-string)
19
19
  * @param domainId a sufficiently unique domain ID in which row-Id's are unique
20
20
  * @param minLength minimum length of nonce and crypto
21
- * @param randomIds whether hash values should remain static per row or random values
21
+ * @param staticIds whether hash values should remain static per row or random values
22
22
  *
23
23
  */
24
- constructor(key: string, domainId: string, minLength?: number, randomIds?: boolean);
24
+ constructor(key: string, domainId: string, minLength?: number, staticIds?: boolean);
25
25
  /**
26
26
  * Encode given id value as a hashid either using random data or given seed value for nonce.
27
27
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oino-ts/hashid",
3
- "version": "0.0.13",
3
+ "version": "0.0.15",
4
4
  "description": "OINO TS package for hashid's.",
5
5
  "author": "Matias Kiviniemi (pragmatta)",
6
6
  "license": "MPL-2.0",
@@ -18,7 +18,7 @@
18
18
  "types": "./dist/types/index.d.ts",
19
19
  "dependencies": {
20
20
  "@types/node": "^20.12.7",
21
- "@oino-ts/types": "^0.0.13",
21
+ "@oino-ts/types": "^0.0.15",
22
22
  "base-x": "5.0.0"
23
23
  },
24
24
  "devDependencies": {
package/src/OINOHashid.ts CHANGED
@@ -26,7 +26,7 @@ export class OINOHashid {
26
26
  private _iv:Buffer
27
27
  private _domainId:string
28
28
  private _minLength:number
29
- private _randomIds
29
+ private _staticIds
30
30
 
31
31
  /**
32
32
  * Hashid constructor
@@ -34,10 +34,10 @@ export class OINOHashid {
34
34
  * @param key AES128 key (32 char hex-string)
35
35
  * @param domainId a sufficiently unique domain ID in which row-Id's are unique
36
36
  * @param minLength minimum length of nonce and crypto
37
- * @param randomIds whether hash values should remain static per row or random values
37
+ * @param staticIds whether hash values should remain static per row or random values
38
38
  *
39
39
  */
40
- constructor (key: string, domainId:string, minLength:number = HASHID_MIN_LENGTH, randomIds:boolean = false) {
40
+ constructor (key: string, domainId:string, minLength:number = HASHID_MIN_LENGTH, staticIds:boolean = false) {
41
41
  this._domainId = domainId
42
42
  if ((minLength < HASHID_MIN_LENGTH) || (minLength > HASHID_MAX_LENGTH)) {
43
43
  throw Error("OINOHashid minLength needs to be between " + HASHID_MIN_LENGTH + " and " + HASHID_MAX_LENGTH + "!")
@@ -46,7 +46,7 @@ export class OINOHashid {
46
46
  if (key.length != 32) {
47
47
  throw Error("OINOHashid key needs to be a 32 character hex-string!")
48
48
  }
49
- this._randomIds = randomIds
49
+ this._staticIds = staticIds
50
50
  this._key = Buffer.from(key, 'hex')
51
51
  this._iv = new Buffer(16)
52
52
  }
@@ -62,14 +62,14 @@ export class OINOHashid {
62
62
 
63
63
  // if seed was given use it for pseudorandom chars, otherwise generate them
64
64
  let random_chars:string = ""
65
- if (this._randomIds) {
66
- randomFillSync(this._iv, 0, 16)
67
- random_chars = hashidEncoder.encode(this._iv) // this._iv.toString('base64url')
68
-
69
- } else {
65
+ if (this._staticIds) {
70
66
  const hmac_seed = createHmac('sha1', this._key)
71
67
  hmac_seed.update(this._domainId + " " + cellSeed)
72
68
  random_chars = hashidEncoder.encode(hmac_seed.digest()) // hmac_seed.digest('base64url')
69
+
70
+ } else {
71
+ randomFillSync(this._iv, 0, 16)
72
+ random_chars = hashidEncoder.encode(this._iv) // this._iv.toString('base64url')
73
73
  }
74
74
  const hmac = createHmac('sha1', this._key)
75
75
  let iv_seed:string = random_chars.substring(0, this._minLength)