-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
1 parent
b565ee3
commit 08c0969
Showing
9 changed files
with
97 additions
and
34 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
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 |
---|---|---|
@@ -1,21 +1,30 @@ | ||
import crypto from 'node:crypto'; | ||
import crypto from 'crypto'; | ||
import { pipeline } from 'stream/promises'; | ||
import type { LiteralUnion } from 'type-fest'; | ||
|
||
const defaultAlgorithm: AlgorithmName = 'sha512'; | ||
|
||
export type AlgorithmName = LiteralUnion< | ||
'sha1' | 'sha224' | 'sha256' | 'sha384' | 'sha512', | ||
string | ||
>; | ||
|
||
export function hash(data: string | Buffer, algorithm?: AlgorithmName): string { | ||
const selectedAlgorithm = algorithm ?? defaultAlgorithm; | ||
|
||
const hash = crypto.createHash(selectedAlgorithm); | ||
export function hash( | ||
data: string | Buffer, | ||
algorithm: AlgorithmName = 'sha512' | ||
): string { | ||
const hash = crypto.createHash(algorithm); | ||
hash.update(data); | ||
return hash.digest('hex'); | ||
} | ||
|
||
export function toSha256(input: string): string { | ||
return hash(input, 'sha256'); | ||
} | ||
|
||
export async function hashStream( | ||
inputStream: NodeJS.ReadableStream, | ||
algorithm: AlgorithmName = 'sha512' | ||
): Promise<string> { | ||
const hash = crypto.createHash(algorithm); | ||
await pipeline(inputStream, hash); | ||
return hash.digest('hex'); | ||
} |
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 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,30 @@ | ||
import crypto from 'node:crypto'; | ||
import fs from 'node:fs/promises'; | ||
|
||
/** | ||
* Generate hash from array of strings | ||
* @param {string[]} input | ||
* @param {string} algorithm | ||
* @returns {string} | ||
*/ | ||
export function hashFromArray(input, algorithm = 'sha512') { | ||
const hash = crypto.createHash(algorithm); | ||
for (const str of input) { | ||
hash.update(str); | ||
} | ||
|
||
return hash.digest('hex'); | ||
} | ||
|
||
/** | ||
* Generate hash from array of strings | ||
* @param {string} file | ||
* @param {string} algorithm | ||
* @returns {Promise<string>} | ||
*/ | ||
export async function hashFile(file, algorithm = 'sha512') { | ||
const data = await fs.readFile(file); | ||
const hash = crypto.createHash(algorithm); | ||
hash.update(data); | ||
return hash.digest('hex'); | ||
} |