|
| 1 | +import { createHash } from 'node:crypto'; |
| 2 | +import type { |
| 3 | + BinaryLike, |
| 4 | + BinaryToTextEncoding, |
| 5 | +} from 'node:crypto'; |
| 6 | + |
1 | 7 | /** |
2 | | - * This file provides a set of functions for creating hash digests using different algorithms and bit lengths. |
3 | | - * It includes functions for generating SHA-3 hash digests with bit lengths of 224, 256, 384, and 512, |
4 | | - * as well as a function for generating MD5 hash digests. |
| 8 | + * Computes the MD5 hash of the given data. |
| 9 | + * |
| 10 | + * @param {BinaryLike} data - The input data to hash |
| 11 | + * @param {BinaryToTextEncoding} [outputEncoding] - The output encoding (default: `'hex'`) |
5 | 12 | * |
6 | | - * These functions use the Node.js crypto module to generate the hashes. |
7 | | - * Can only be used in Node.js/Deno/Bun runtimes. |
| 13 | + * @returns {string | Buffer} The hash digest in the specified encoding |
8 | 14 | * |
9 | 15 | * @example |
10 | 16 | * ```typescript |
11 | | - * import { cryptoSha3256 } from '@kikiutils/shared/crypto-hash'; |
| 17 | + * import { cryptoMd5 } from '@kikiutils/shared/crypto-hash'; |
12 | 18 | * |
13 | | - * console.log(cryptoSha3256('test')); // 36f028580bb02cc8272a9a020f4200e346e276ae664e45ee80745574e2f5ab80 |
| 19 | + * console.log(cryptoMd5('test')); // 33ed9a05a10b21f0... |
14 | 20 | * ``` |
15 | 21 | */ |
16 | | - |
17 | | -import { createHash } from 'node:crypto'; |
18 | | -import type { |
19 | | - BinaryLike, |
20 | | - BinaryToTextEncoding, |
21 | | -} from 'node:crypto'; |
22 | | - |
23 | 22 | export function cryptoMd5(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') { |
24 | 23 | return createHash('md5').update(data).digest(outputEncoding); |
25 | 24 | } |
26 | 25 |
|
| 26 | +/** |
| 27 | + * Computes the MD5 hash of the given data and returns the raw buffer. |
| 28 | + * |
| 29 | + * @param {BinaryLike} data - The input data to hash |
| 30 | + * |
| 31 | + * @returns {Buffer} The raw hash digest as a Buffer |
| 32 | + * |
| 33 | + * @example |
| 34 | + * ```typescript |
| 35 | + * import { cryptoMd5ToBuffer } from '@kikiutils/shared/crypto-hash'; |
| 36 | + * |
| 37 | + * console.log(cryptoMd5ToBuffer('test')); // <Buffer 33 ed 9a ...> |
| 38 | + * ``` |
| 39 | + */ |
27 | 40 | export function cryptoMd5ToBuffer(data: BinaryLike) { |
28 | 41 | return createHash('md5').update(data).digest(); |
29 | 42 | } |
30 | 43 |
|
| 44 | +/** |
| 45 | + * Computes the SHA-3 hash of the given data using the 224-bit digest. |
| 46 | + * |
| 47 | + * @param {BinaryLike} data - The input data to hash |
| 48 | + * @param {BinaryToTextEncoding} [outputEncoding] - The output encoding (default: `'hex'`) |
| 49 | + * |
| 50 | + * @returns {string | Buffer} The hash digest in the specified encoding |
| 51 | + * |
| 52 | + * @example |
| 53 | + * ```typescript |
| 54 | + * import { cryptoSha3224 } from '@kikiutils/shared/crypto-hash'; |
| 55 | + * |
| 56 | + * console.log(cryptoSha3224('test')); // 0qW8FoqDcFmkpuqR9J3uT8YX... |
| 57 | + * ``` |
| 58 | + */ |
31 | 59 | export function cryptoSha3224(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') { |
32 | 60 | return createHash('sha3-224').update(data).digest(outputEncoding); |
33 | 61 | } |
34 | 62 |
|
| 63 | +/** |
| 64 | + * Computes the SHA-3 hash of the given data using the 224-bit digest and returns the raw buffer. |
| 65 | + * |
| 66 | + * @param {BinaryLike} data - The input data to hash |
| 67 | + * |
| 68 | + * @returns {Buffer} The raw hash digest as a Buffer |
| 69 | + * |
| 70 | + * @example |
| 71 | + * ```typescript |
| 72 | + * import { cryptoSha3224ToBuffer } from '@kikiutils/shared/crypto-hash'; |
| 73 | + * |
| 74 | + * console.log(cryptoSha3224ToBuffer('test')); // <Buffer 0q W8 Fo ...> |
| 75 | + * ``` |
| 76 | + */ |
35 | 77 | export function cryptoSha3224ToBuffer(data: BinaryLike) { |
36 | 78 | return createHash('sha3-224').update(data).digest(); |
37 | 79 | } |
38 | 80 |
|
| 81 | +/** |
| 82 | + * Computes the SHA-3 hash of the given data using the 256-bit digest. |
| 83 | + * |
| 84 | + * @param {BinaryLike} data - The input data to hash |
| 85 | + * @param {BinaryToTextEncoding} [outputEncoding] - The output encoding (default: `'hex'`) |
| 86 | + * |
| 87 | + * @returns {string | Buffer} The hash digest in the specified encoding |
| 88 | + * |
| 89 | + * @example |
| 90 | + * ```typescript |
| 91 | + * import { cryptoSha3256 } from '@kikiutils/shared/crypto-hash'; |
| 92 | + * |
| 93 | + * console.log(cryptoSha3256('test')); // 36f028580bb02cc8272a9a020f4200e346e276ae664e45ee80745574e2f5ab80 |
| 94 | + * ``` |
| 95 | + */ |
39 | 96 | export function cryptoSha3256(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') { |
40 | 97 | return createHash('sha3-256').update(data).digest(outputEncoding); |
41 | 98 | } |
42 | 99 |
|
| 100 | +/** |
| 101 | + * Computes the SHA-3 hash of the given data using the 256-bit digest and returns the raw buffer. |
| 102 | + * |
| 103 | + * @param {BinaryLike} data - The input data to hash |
| 104 | + * |
| 105 | + * @returns {Buffer} The raw hash digest as a Buffer |
| 106 | + * |
| 107 | + * @example |
| 108 | + * ```typescript |
| 109 | + * import { cryptoSha3256ToBuffer } from '@kikiutils/shared/crypto-hash'; |
| 110 | + * |
| 111 | + * console.log(cryptoSha3256ToBuffer('test')); // <Buffer 36 f0 28 ...> |
| 112 | + * ``` |
| 113 | + */ |
43 | 114 | export function cryptoSha3256ToBuffer(data: BinaryLike) { |
44 | 115 | return createHash('sha3-256').update(data).digest(); |
45 | 116 | } |
46 | 117 |
|
| 118 | +/** |
| 119 | + * Computes the SHA-3 hash of the given data using the 384-bit digest. |
| 120 | + * |
| 121 | + * @param {BinaryLike} data - The input data to hash |
| 122 | + * @param {BinaryToTextEncoding} [outputEncoding] - The output encoding (default: `'hex'`) |
| 123 | + * |
| 124 | + * @returns {string | Buffer} The hash digest in the specified encoding |
| 125 | + * |
| 126 | + * @example |
| 127 | + * ```typescript |
| 128 | + * import { cryptoSha3384 } from '@kikiutils/shared/crypto-hash'; |
| 129 | + * |
| 130 | + * console.log(cryptoSha3384('test')); // 1Q2wQAYaZ4o8vWX5bEB2n3Y... |
| 131 | + * ``` |
| 132 | + */ |
47 | 133 | export function cryptoSha3384(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') { |
48 | 134 | return createHash('sha3-384').update(data).digest(outputEncoding); |
49 | 135 | } |
50 | 136 |
|
| 137 | +/** |
| 138 | + * Computes the SHA-3 hash of the given data using the 384-bit digest and returns the raw buffer. |
| 139 | + * |
| 140 | + * @param {BinaryLike} data - The input data to hash |
| 141 | + * |
| 142 | + * @returns {Buffer} The raw hash digest as a Buffer |
| 143 | + * |
| 144 | + * @example |
| 145 | + * ```typescript |
| 146 | + * import { cryptoSha3384ToBuffer } from '@kikiutils/shared/crypto-hash'; |
| 147 | + * |
| 148 | + * console.log(cryptoSha3384ToBuffer('test')); // <Buffer 1Q 2w QZ ...> |
| 149 | + * ``` |
| 150 | + */ |
51 | 151 | export function cryptoSha3384ToBuffer(data: BinaryLike) { |
52 | 152 | return createHash('sha3-384').update(data).digest(); |
53 | 153 | } |
54 | 154 |
|
| 155 | +/** |
| 156 | + * Computes the SHA-3 hash of the given data using the 512-bit digest. |
| 157 | + * |
| 158 | + * @param {BinaryLike} data - The input data to hash |
| 159 | + * @param {BinaryToTextEncoding} [outputEncoding] - The output encoding (default: `'hex'`) |
| 160 | + * |
| 161 | + * @returns {string | Buffer} The hash digest in the specified encoding |
| 162 | + * |
| 163 | + * @example |
| 164 | + * ```typescript |
| 165 | + * import { cryptoSha3512 } from '@kikiutils/shared/crypto-hash'; |
| 166 | + * |
| 167 | + * console.log(cryptoSha3512('test')); // b_NQJ8FJBY9dIjm1q7... |
| 168 | + * ``` |
| 169 | + */ |
55 | 170 | export function cryptoSha3512(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') { |
56 | 171 | return createHash('sha3-512').update(data).digest(outputEncoding); |
57 | 172 | } |
58 | 173 |
|
| 174 | +/** |
| 175 | + * Computes the SHA-3 hash of the given data using the 512-bit digest and returns the raw buffer. |
| 176 | + * |
| 177 | + * @param {BinaryLike} data - The input data to hash |
| 178 | + * |
| 179 | + * @returns {Buffer} The raw hash digest as a Buffer |
| 180 | + * |
| 181 | + * @example |
| 182 | + * ```typescript |
| 183 | + * import { cryptoSha3512ToBuffer } from '@kikiutils/shared/crypto-hash'; |
| 184 | + * |
| 185 | + * console.log(cryptoSha3512ToBuffer('test')); // <Buffer b_ NQ ...> |
| 186 | + * ``` |
| 187 | + */ |
59 | 188 | export function cryptoSha3512ToBuffer(data: BinaryLike) { |
60 | 189 | return createHash('sha3-512').update(data).digest(); |
61 | 190 | } |
0 commit comments