Debian Codegraph @

hasha/readme.md

raw
1 <h1 align="center">
2 <br>
3 <br>
4 <br>
5 <img width="380" src="media/logo.svg" alt="hasha">
6 <br>
7 <br>
8 <br>
9 <br>
10 <br>
11 </h1>
12
13 > Hashing made simple. Get the hash of a buffer/string/stream/file.
14
15 [![Build Status](https://travis-ci.com/sindresorhus/hasha.svg?branch=master)](https://travis-ci.com/github/sindresorhus/hasha)
16
17 Convenience wrapper around the core [`crypto` Hash class](https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm) with simpler API and better defaults.
18
19 ## Install
20
21 ```
22 $ npm install hasha
23 ```
24
25 ## Usage
26
27 ```js
28 const hasha = require('hasha');
29
30 hasha('unicorn');
31 //=> 'e233b19aabc7d5e53826fb734d1222f1f0444c3a3fc67ff4af370a66e7cadd2cb24009f1bc86f0bed12ca5fcb226145ad10fc5f650f6ef0959f8aadc5a594b27'
32 ```
33
34 ```js
35 const hasha = require('hasha');
36
37 (async () => {
38 console.log(await hasha.async('unicorn'));
39 //=> 'e233b19aabc7d5e53826fb734d1222f1f0444c3a3fc67ff4af370a66e7cadd2cb24009f1bc86f0bed12ca5fcb226145ad10fc5f650f6ef0959f8aadc5a594b27'
40 })();
41 ```
42
43 ```js
44 const hasha = require('hasha');
45
46 // Hash the process input and output the hash sum
47 process.stdin.pipe(hasha.stream()).pipe(process.stdout);
48 ```
49
50 ```js
51 const hasha = require('hasha');
52
53 (async () => {
54 // Get the MD5 hash of an image
55 const hash = await hasha.fromFile('unicorn.png', {algorithm: 'md5'});
56
57 console.log(hash);
58 //=> '1abcb33beeb811dca15f0ac3e47b88d9'
59 })();
60 ```
61
62 ## API
63
64 See the Node.js [`crypto` docs](https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm_options) for more about hashing.
65
66 ### hasha(input, options?)
67
68 Returns a hash.
69
70 #### input
71
72 Type: `Buffer | string | Array<Buffer | string>`
73
74 Buffer you want to hash.
75
76 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.
77
78 Pass an array instead of concatenating strings and/or buffers. The output is the same, but arrays do not incur the overhead of concatenation.
79
80 #### options
81
82 Type: `object`
83
84 ##### encoding
85
86 Type: `string`\
87 Default: `'hex'`\
88 Values: `'hex' | 'base64' | 'buffer' | 'latin1'`
89
90 Encoding of the returned hash.
91
92 ##### algorithm
93
94 Type: `string`\
95 Default: `'sha512'`\
96 Values: `'md5' | 'sha1' | 'sha256' | 'sha512'` *([Platform dependent](https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm_options))*
97
98 *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)*
99
100 ### hasha.async(input, options?)
101
102 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.
103
104 Returns a hash asynchronously.
105
106 ### hasha.stream(options?)
107
108 Returns a [hash transform stream](https://nodejs.org/api/crypto.html#crypto_class_hash).
109
110 ### hasha.fromStream(stream, options?)
111
112 Returns a `Promise` for the calculated hash.
113
114 ### hasha.fromFile(filepath, options?)
115
116 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.
117
118 Returns a `Promise` for the calculated file hash.
119
120 ### hasha.fromFileSync(filepath, options?)
121
122 Returns the calculated file hash.
123
124 ## Related
125
126 - [hasha-cli](https://github.com/sindresorhus/hasha-cli) - CLI for this module
127 - [crypto-hash](https://github.com/sindresorhus/crypto-hash) - Tiny hashing module that uses the native crypto API in Node.js and the browser
128 - [hash-obj](https://github.com/sindresorhus/hash-obj) - Get the hash of an object
129 - [md5-hex](https://github.com/sindresorhus/md5-hex) - Create a MD5 hash with hex encoding