This repository has been archived by the owner on Mar 9, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
321 additions
and
369 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ artifacts/ | |
docs/ | ||
.nyc_output/ | ||
logs/ | ||
dist/ | ||
|
||
*.log | ||
*.tap | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
circle.yml | ||
.travis.yml | ||
test/ | ||
src/ | ||
coverage/ | ||
tsconfig.json | ||
tsconfig.build.json | ||
artifacts/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
var Skip32: any = require('skip32').Skip32; | ||
var Hashids: any = require('hashids'); | ||
|
||
// Public methods | ||
|
||
export class Concealer { | ||
|
||
private skip32: any; | ||
private hashids: any; | ||
|
||
constructor(private secretKey: number[], private salt: string, | ||
private minLength = 1, private customAlphabet?: string) { | ||
|
||
if (!secretKey) { | ||
throw new Error('Secret Key must be provided'); | ||
} | ||
if (!salt) { | ||
throw new Error('Salt must be provided'); | ||
} | ||
if (!Array.isArray(secretKey) || !secretKey.length || !secretKey.every(isByte)) { | ||
throw new TypeError('Secret Key must be an Array of bytes represented by integers'); | ||
} | ||
if (minLength !== undefined && !Number.isInteger(minLength)) { | ||
throw new TypeError('Min Length must be an integer, if provided'); | ||
} | ||
|
||
this.skip32 = new Skip32(secretKey); | ||
this.hashids = new Hashids(salt, minLength || 1, customAlphabet); | ||
} | ||
|
||
|
||
encode(key: number): string { | ||
if (!Number.isInteger(key) || key < 0) { | ||
throw new TypeError('Key must be a non-negative integer'); | ||
} | ||
|
||
const encrypted = this.skip32.encrypt(key); | ||
|
||
return this.hashids.encode(encrypted); | ||
} | ||
|
||
|
||
decode(key: string): number { | ||
const encrypted = this.hashids.decode(key)[0]; | ||
|
||
if (encrypted === undefined) { | ||
return null; | ||
} | ||
|
||
return this.skip32.decrypt(encrypted); | ||
} | ||
} | ||
|
||
|
||
// Private methods | ||
|
||
// Returns whether a given value represents a single byte | ||
function isByte(val: number) { | ||
return Number.isInteger(val) && val >= 0 && val <= 255; | ||
}; |
Oops, something went wrong.