| 1 |
/// <reference types="node"/> |
| 2 |
import {LiteralUnion} from 'type-fest'; |
| 3 |
import {Hash} from 'crypto'; |
| 4 |
|
| 5 |
declare namespace hasha { |
| 6 |
type ToStringEncoding = 'hex' | 'base64' | 'latin1'; |
| 7 |
type HashaInput = Buffer | string | Array<Buffer | string>; |
| 8 |
type HashaEncoding = ToStringEncoding | 'buffer'; |
| 9 |
|
| 10 |
type AlgorithmName = LiteralUnion< |
| 11 |
'md5' | 'sha1' | 'sha256' | 'sha512', |
| 12 |
string |
| 13 |
>; |
| 14 |
|
| 15 |
interface Options<EncodingType = HashaEncoding> { |
| 16 |
/** |
| 17 |
Encoding of the returned hash. |
| 18 |
|
| 19 |
@default 'hex' |
| 20 |
*/ |
| 21 |
readonly encoding?: EncodingType; |
| 22 |
|
| 23 |
/** |
| 24 |
Values: `md5` `sha1` `sha256` `sha512` _([Platform dependent](https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm_options))_ |
| 25 |
|
| 26 |
_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)_ |
| 27 |
|
| 28 |
@default 'sha512' |
| 29 |
*/ |
| 30 |
readonly algorithm?: AlgorithmName; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
declare const hasha: { |
| 35 |
/** |
| 36 |
Calculate the hash for a `string`, `Buffer`, or an array thereof. |
| 37 |
|
| 38 |
@param input - Data you want to hash. |
| 39 |
|
| 40 |
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. |
| 41 |
|
| 42 |
Pass an array instead of concatenating strings and/or buffers. The output is the same, but arrays do not incur the overhead of concatenation. |
| 43 |
|
| 44 |
@returns A hash. |
| 45 |
|
| 46 |
@example |
| 47 |
``` |
| 48 |
import hasha = require('hasha'); |
| 49 |
|
| 50 |
hasha('unicorn'); |
| 51 |
//=> 'e233b19aabc7d5e53826fb734d1222f1f0444c3a3fc67ff4af370a66e7cadd2cb24009f1bc86f0bed12ca5fcb226145ad10fc5f650f6ef0959f8aadc5a594b27' |
| 52 |
``` |
| 53 |
*/ |
| 54 |
(input: hasha.HashaInput): string; |
| 55 |
( |
| 56 |
input: hasha.HashaInput, |
| 57 |
options: hasha.Options<hasha.ToStringEncoding> |
| 58 |
): string; |
| 59 |
(input: hasha.HashaInput, options: hasha.Options<'buffer'>): Buffer; |
| 60 |
|
| 61 |
/** |
| 62 |
Asynchronously calculate the hash for a `string`, `Buffer`, or an array thereof. |
| 63 |
|
| 64 |
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. |
| 65 |
|
| 66 |
@param input - Data you want to hash. |
| 67 |
|
| 68 |
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. |
| 69 |
|
| 70 |
Pass an array instead of concatenating strings and/or buffers. The output is the same, but arrays do not incur the overhead of concatenation. |
| 71 |
|
| 72 |
@returns A hash. |
| 73 |
|
| 74 |
@example |
| 75 |
``` |
| 76 |
import hasha = require('hasha'); |
| 77 |
|
| 78 |
(async () => { |
| 79 |
console.log(await hasha.async('unicorn')); |
| 80 |
//=> 'e233b19aabc7d5e53826fb734d1222f1f0444c3a3fc67ff4af370a66e7cadd2cb24009f1bc86f0bed12ca5fcb226145ad10fc5f650f6ef0959f8aadc5a594b27' |
| 81 |
})(); |
| 82 |
``` |
| 83 |
*/ |
| 84 |
async(input: hasha.HashaInput): Promise<string>; |
| 85 |
async( |
| 86 |
input: hasha.HashaInput, |
| 87 |
options: hasha.Options<hasha.ToStringEncoding> |
| 88 |
): Promise<string>; |
| 89 |
async(input: hasha.HashaInput, options: hasha.Options<'buffer'>): Promise<Buffer>; |
| 90 |
|
| 91 |
/** |
| 92 |
Create a [hash transform stream](https://nodejs.org/api/crypto.html#crypto_class_hash). |
| 93 |
|
| 94 |
@returns The created hash transform stream. |
| 95 |
|
| 96 |
@example |
| 97 |
``` |
| 98 |
import hasha = require('hasha'); |
| 99 |
|
| 100 |
// Hash the process input and output the hash sum |
| 101 |
process.stdin.pipe(hasha.stream()).pipe(process.stdout); |
| 102 |
``` |
| 103 |
*/ |
| 104 |
stream(options?: hasha.Options<hasha.HashaEncoding>): Hash; |
| 105 |
|
| 106 |
/** |
| 107 |
Calculate the hash for a stream. |
| 108 |
|
| 109 |
@param stream - A stream you want to hash. |
| 110 |
@returns The calculated hash. |
| 111 |
*/ |
| 112 |
fromStream(stream: NodeJS.ReadableStream): Promise<string>; |
| 113 |
fromStream( |
| 114 |
stream: NodeJS.ReadableStream, |
| 115 |
options?: hasha.Options<hasha.ToStringEncoding> |
| 116 |
): Promise<string>; |
| 117 |
fromStream( |
| 118 |
stream: NodeJS.ReadableStream, |
| 119 |
options?: hasha.Options<'buffer'> |
| 120 |
): Promise<Buffer>; |
| 121 |
|
| 122 |
/** |
| 123 |
Calculate the hash for a file. |
| 124 |
|
| 125 |
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. |
| 126 |
|
| 127 |
@param filePath - Path to a file you want to hash. |
| 128 |
@returns The calculated file hash. |
| 129 |
|
| 130 |
@example |
| 131 |
``` |
| 132 |
import hasha = require('hasha'); |
| 133 |
|
| 134 |
(async () => { |
| 135 |
// Get the MD5 hash of an image |
| 136 |
const hash = await hasha.fromFile('unicorn.png', {algorithm: 'md5'}); |
| 137 |
|
| 138 |
console.log(hash); |
| 139 |
//=> '1abcb33beeb811dca15f0ac3e47b88d9' |
| 140 |
})(); |
| 141 |
``` |
| 142 |
*/ |
| 143 |
fromFile(filePath: string): Promise<string>; |
| 144 |
fromFile( |
| 145 |
filePath: string, |
| 146 |
options: hasha.Options<hasha.ToStringEncoding> |
| 147 |
): Promise<string>; |
| 148 |
fromFile( |
| 149 |
filePath: string, |
| 150 |
options: hasha.Options<'buffer'> |
| 151 |
): Promise<Buffer>; |
| 152 |
|
| 153 |
/** |
| 154 |
Synchronously calculate the hash for a file. |
| 155 |
|
| 156 |
@param filePath - Path to a file you want to hash. |
| 157 |
@returns The calculated file hash. |
| 158 |
*/ |
| 159 |
fromFileSync(filePath: string): string; |
| 160 |
fromFileSync( |
| 161 |
filePath: string, |
| 162 |
options: hasha.Options<hasha.ToStringEncoding> |
| 163 |
): string; |
| 164 |
fromFileSync(filePath: string, options: hasha.Options<'buffer'>): Buffer; |
| 165 |
}; |
| 166 |
|
| 167 |
export = hasha; |