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
Sebastian Niemann
committed
Oct 27, 2019
1 parent
b123169
commit d9c0a66
Showing
9 changed files
with
92 additions
and
78 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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import * as asmCrypto from 'asmcrypto.js'; | ||
|
||
function createHash(algorithm: string) { | ||
return new Hasher(algorithm); | ||
} | ||
|
||
class Hasher { | ||
hasher: asmCrypto.Sha256; | ||
|
||
constructor(algorithm: string) { | ||
switch (algorithm) { | ||
case 'sha256': | ||
this.hasher = new asmCrypto.Sha256(); | ||
break; | ||
default: | ||
throw new Error(`Unsupported algorithm: '${algorithm}'.`); | ||
} | ||
} | ||
|
||
digest(encoding: string) { | ||
this.hasher.finish(); | ||
// The type of '.result' is always 'Uint8Array' after calling 'finish()'. Before that, it could also be 'null'. Casting | ||
// is necessary to avoid type errors when encoding the hash. | ||
const hash = this.hasher.result as Uint8Array; | ||
|
||
let encoded: string; | ||
switch (encoding) { | ||
case 'hex': | ||
encoded = asmCrypto.bytes_to_hex(hash); | ||
break; | ||
default: | ||
throw new Error(`Unsupported encoding: '${encoding}'.`); | ||
} | ||
|
||
return encoded; | ||
} | ||
|
||
update(data: string | Buffer) { | ||
if (typeof data === 'string') { | ||
this.hasher.process(new TextEncoder().encode(data)); | ||
} else { | ||
this.hasher.process(data); | ||
} | ||
|
||
return this; | ||
} | ||
} | ||
|
||
export { createHash }; |
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 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
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export { createHash } from 'crypto'; |
This file was deleted.
Oops, something went wrong.