-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
322 additions
and
364 deletions.
There are no files selected for viewing
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
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,167 +1,125 @@ | ||
/// <reference types="node"/> | ||
import {LiteralUnion} from 'type-fest'; | ||
import {Hash} from 'crypto'; | ||
import {type Hash} from 'node:crypto'; | ||
import {type LiteralUnion} from 'type-fest'; | ||
|
||
declare namespace hasha { | ||
type ToStringEncoding = 'hex' | 'base64' | 'latin1'; | ||
type HashaInput = Buffer | string | Array<Buffer | string>; | ||
type HashaEncoding = ToStringEncoding | 'buffer'; | ||
export type HashInput = HashSyncInput | NodeJS.ReadableStream; | ||
export type HashSyncInput = Uint8Array | string | Array<Uint8Array | string>; | ||
export type StringEncoding = 'hex' | 'base64' | 'latin1'; | ||
export type HashEncoding = StringEncoding | 'buffer'; | ||
|
||
type AlgorithmName = LiteralUnion< | ||
'md5' | 'sha1' | 'sha256' | 'sha512', | ||
string | ||
>; | ||
export type HashAlgorithm = LiteralUnion< | ||
'md5' | 'sha1' | 'sha256' | 'sha512', | ||
string | ||
>; | ||
|
||
interface Options<EncodingType = HashaEncoding> { | ||
/** | ||
Encoding of the returned hash. | ||
@default 'hex' | ||
*/ | ||
readonly encoding?: EncodingType; | ||
|
||
/** | ||
Values: `md5` `sha1` `sha256` `sha512` _([Platform dependent](https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm_options))_ | ||
_The `md5` algorithm is good for [file revving](https://github.com/sindresorhus/rev-hash), but you should never use `md5` or `sha1` for anything sensitive. [They're insecure.](https://security.googleblog.com/2014/09/gradually-sunsetting-sha-1.html)_ | ||
export type Options<EncodingType extends HashEncoding = 'hex'> = { | ||
/** | ||
The encoding of the returned hash. | ||
@default 'sha512' | ||
*/ | ||
readonly algorithm?: AlgorithmName; | ||
} | ||
} | ||
@default 'hex' | ||
*/ | ||
readonly encoding?: EncodingType; | ||
|
||
declare const hasha: { | ||
/** | ||
Calculate the hash for a `string`, `Buffer`, or an array thereof. | ||
The available values are [platform dependent](https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm_options). | ||
@param input - Data you want to hash. | ||
_The `md5` algorithm is good for [file revving](https://github.com/sindresorhus/rev-hash), but you should never use `md5` or `sha1` for anything sensitive. [They're insecure.](https://security.googleblog.com/2014/09/gradually-sunsetting-sha-1.html)_ | ||
While strings are supported you should prefer buffers as they're faster to hash. Although if you already have a string you should not convert it to a buffer. | ||
@default 'sha512' | ||
*/ | ||
readonly algorithm?: HashAlgorithm; | ||
}; | ||
|
||
Pass an array instead of concatenating strings and/or buffers. The output is the same, but arrays do not incur the overhead of concatenation. | ||
/** | ||
Calculate the hash of a `string`, `Uint8Array`, or an array thereof. | ||
@returns A hash. | ||
@param input - The value to hash. | ||
@returns A hash. | ||
@example | ||
``` | ||
import hasha = require('hasha'); | ||
The operation is executed using `worker_threads`. A thread is lazily spawned on the first operation and lives until the end of the program execution. It's unrefed, so it won't keep the process alive. | ||
hasha('unicorn'); | ||
//=> 'e233b19aabc7d5e53826fb734d1222f1f0444c3a3fc67ff4af370a66e7cadd2cb24009f1bc86f0bed12ca5fcb226145ad10fc5f650f6ef0959f8aadc5a594b27' | ||
``` | ||
*/ | ||
(input: hasha.HashaInput): string; | ||
( | ||
input: hasha.HashaInput, | ||
options: hasha.Options<hasha.ToStringEncoding> | ||
): string; | ||
(input: hasha.HashaInput, options: hasha.Options<'buffer'>): Buffer; | ||
While strings are supported you should prefer buffers as they're faster to hash. Although if you already have a string you should not convert it to a buffer. | ||
/** | ||
Asynchronously calculate the hash for a `string`, `Buffer`, or an array thereof. | ||
Pass an array instead of concatenating strings and/or buffers. The output is the same, but arrays do not incur the overhead of concatenation. | ||
In Node.js 12 or later, the operation is executed using `worker_threads`. A thread is lazily spawned on the first operation and lives until the end of the program execution. It's unrefed, so it won't keep the process alive. | ||
@example | ||
``` | ||
import {hash} from 'hasha'; | ||
@param input - Data you want to hash. | ||
await hash('unicorn'); | ||
//=> 'e233b19aabc7d5e53826fb734d1222f1f0444c3a3fc67ff4af370a66e7cadd2cb24009f1bc86f0bed12ca5fcb226145ad10fc5f650f6ef0959f8aadc5a594b27' | ||
``` | ||
*/ | ||
export function hash(input: HashInput, options?: Options<StringEncoding>): Promise<string>; | ||
export function hash(input: HashInput, options?: Options<'buffer'>): Promise<Buffer>; | ||
|
||
While strings are supported you should prefer buffers as they're faster to hash. Although if you already have a string you should not convert it to a buffer. | ||
/** | ||
Synchronously calculate the hash of a `string`, `Uint8Array`, or an array thereof. | ||
Pass an array instead of concatenating strings and/or buffers. The output is the same, but arrays do not incur the overhead of concatenation. | ||
@param input - The value to hash. | ||
@returns A hash. | ||
@returns A hash. | ||
While strings are supported you should prefer buffers as they're faster to hash. Although if you already have a string you should not convert it to a buffer. | ||
@example | ||
``` | ||
import hasha = require('hasha'); | ||
Pass an array instead of concatenating strings and/or buffers. The output is the same, but arrays do not incur the overhead of concatenation. | ||
(async () => { | ||
console.log(await hasha.async('unicorn')); | ||
//=> 'e233b19aabc7d5e53826fb734d1222f1f0444c3a3fc67ff4af370a66e7cadd2cb24009f1bc86f0bed12ca5fcb226145ad10fc5f650f6ef0959f8aadc5a594b27' | ||
})(); | ||
``` | ||
*/ | ||
async(input: hasha.HashaInput): Promise<string>; | ||
async( | ||
input: hasha.HashaInput, | ||
options: hasha.Options<hasha.ToStringEncoding> | ||
): Promise<string>; | ||
async(input: hasha.HashaInput, options: hasha.Options<'buffer'>): Promise<Buffer>; | ||
@example | ||
``` | ||
import {hashSync} from 'hasha'; | ||
/** | ||
Create a [hash transform stream](https://nodejs.org/api/crypto.html#crypto_class_hash). | ||
hashSync('unicorn'); | ||
//=> 'e233b19aabc7d5e53826fb734d1222f1f0444c3a3fc67ff4af370a66e7cadd2cb24009f1bc86f0bed12ca5fcb226145ad10fc5f650f6ef0959f8aadc5a594b27' | ||
``` | ||
*/ | ||
export function hashSync(input: HashSyncInput, options?: Options<StringEncoding>): string; | ||
export function hashSync(input: HashSyncInput, options?: Options<'buffer'>): Buffer; | ||
|
||
@returns The created hash transform stream. | ||
/** | ||
Calculate the hash of a file. | ||
@example | ||
``` | ||
import hasha = require('hasha'); | ||
@param filePath - Path to a file you want to hash. | ||
@returns The calculated file hash. | ||
// Hash the process input and output the hash sum | ||
process.stdin.pipe(hasha.stream()).pipe(process.stdout); | ||
``` | ||
*/ | ||
stream(options?: hasha.Options<hasha.HashaEncoding>): Hash; | ||
|
||
/** | ||
Calculate the hash for a stream. | ||
@param stream - A stream you want to hash. | ||
@returns The calculated hash. | ||
*/ | ||
fromStream(stream: NodeJS.ReadableStream): Promise<string>; | ||
fromStream( | ||
stream: NodeJS.ReadableStream, | ||
options?: hasha.Options<hasha.ToStringEncoding> | ||
): Promise<string>; | ||
fromStream( | ||
stream: NodeJS.ReadableStream, | ||
options?: hasha.Options<'buffer'> | ||
): Promise<Buffer>; | ||
The operation is executed using `worker_threads`. A thread is lazily spawned on the first operation and lives until the end of the program execution. It's unrefed, so it won't keep the process alive. | ||
/** | ||
Calculate the hash for a file. | ||
@example | ||
``` | ||
import {hashFile} from 'hasha'; | ||
In Node.js 12 or later, the operation is executed using `worker_threads`. A thread is lazily spawned on the first operation and lives until the end of the program execution. It's unrefed, so it won't keep the process alive. | ||
// Get the MD5 hash of an image | ||
await hashFile('unicorn.png', {algorithm: 'md5'}); | ||
//=> '1abcb33beeb811dca15f0ac3e47b88d9' | ||
``` | ||
*/ | ||
export function hashFile(filePath: string, options?: Options<StringEncoding>): Promise<string>; | ||
export function hashFile(filePath: string, options?: Options<'buffer'>): Promise<Buffer>; | ||
|
||
@param filePath - Path to a file you want to hash. | ||
@returns The calculated file hash. | ||
/** | ||
Synchronously calculate the hash of a file. | ||
@example | ||
``` | ||
import hasha = require('hasha'); | ||
@param filePath - Path to a file you want to hash. | ||
@returns The calculated file hash. | ||
(async () => { | ||
// Get the MD5 hash of an image | ||
const hash = await hasha.fromFile('unicorn.png', {algorithm: 'md5'}); | ||
@example | ||
``` | ||
import {hashFileSync} from 'hasha'; | ||
console.log(hash); | ||
//=> '1abcb33beeb811dca15f0ac3e47b88d9' | ||
})(); | ||
``` | ||
*/ | ||
fromFile(filePath: string): Promise<string>; | ||
fromFile( | ||
filePath: string, | ||
options: hasha.Options<hasha.ToStringEncoding> | ||
): Promise<string>; | ||
fromFile( | ||
filePath: string, | ||
options: hasha.Options<'buffer'> | ||
): Promise<Buffer>; | ||
// Get the MD5 hash of an image | ||
hashFileSync('unicorn.png', {algorithm: 'md5'}); | ||
//=> '1abcb33beeb811dca15f0ac3e47b88d9' | ||
``` | ||
*/ | ||
export function hashFileSync(filePath: string, options?: Options<StringEncoding>): string; | ||
export function hashFileSync(filePath: string, options?: Options<'buffer'>): Buffer; | ||
|
||
/** | ||
Synchronously calculate the hash for a file. | ||
/** | ||
Create a [hash transform stream](https://nodejs.org/api/crypto.html#crypto_class_hash). | ||
@param filePath - Path to a file you want to hash. | ||
@returns The calculated file hash. | ||
*/ | ||
fromFileSync(filePath: string): string; | ||
fromFileSync( | ||
filePath: string, | ||
options: hasha.Options<hasha.ToStringEncoding> | ||
): string; | ||
fromFileSync(filePath: string, options: hasha.Options<'buffer'>): Buffer; | ||
}; | ||
@example | ||
``` | ||
import {hashingStream} from 'hasha'; | ||
export = hasha; | ||
// Hash the process input and output the hash sum | ||
process.stdin.pipe(hashingStream()).pipe(process.stdout); | ||
``` | ||
*/ | ||
export function hashingStream(options?: Options): Hash; |
Oops, something went wrong.