├── .npmrc ├── .gitattributes ├── .gitignore ├── .github ├── security.md └── workflows │ └── main.yml ├── browser.js ├── .editorconfig ├── index.test-d.ts ├── index.js ├── test.js ├── index.d.ts ├── package.json ├── license └── readme.md /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /browser.js: -------------------------------------------------------------------------------- 1 | import blueimpMd5 from 'blueimp-md5'; 2 | 3 | export default function md5Hex(data) { 4 | if (Array.isArray(data)) { 5 | data = data.join(''); 6 | } 7 | 8 | return blueimpMd5(data); 9 | } 10 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import fs from 'node:fs'; 2 | import {expectType} from 'tsd'; 3 | import md5Hex from './index.js'; 4 | 5 | // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment 6 | const buffer: Uint8Array = fs.readFileSync('unicorn.png'); 7 | 8 | expectType(md5Hex(buffer)); 9 | expectType(md5Hex([buffer])); 10 | expectType(md5Hex('foo')); 11 | expectType(md5Hex(['foo'])); 12 | expectType(md5Hex([buffer, 'foo'])); 13 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import crypto from 'node:crypto'; 2 | 3 | export default function md5Hex(data) { 4 | const hash = crypto.createHash('md5'); 5 | 6 | const update = buffer => { 7 | const inputEncoding = typeof buffer === 'string' ? 'utf8' : undefined; 8 | hash.update(buffer, inputEncoding); 9 | }; 10 | 11 | if (Array.isArray(data)) { 12 | for (const element of data) { 13 | update(element); 14 | } 15 | } else { 16 | update(data); 17 | } 18 | 19 | return hash.digest('hex'); 20 | } 21 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 20 14 | - 18 15 | steps: 16 | - uses: actions/checkout@v4 17 | - uses: actions/setup-node@v4 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import {Buffer} from 'node:buffer'; 2 | import test from 'ava'; 3 | import md5HexBrowser from './browser.js'; 4 | import md5Hex from './index.js'; 5 | 6 | test('main', t => { 7 | t.is(md5Hex('unicorn'), '1abcb33beeb811dca15f0ac3e47b88d9'); 8 | t.is(md5Hex(Buffer.from('unicorn')), '1abcb33beeb811dca15f0ac3e47b88d9'); 9 | t.is(md5Hex(['uni', 'corn']), '1abcb33beeb811dca15f0ac3e47b88d9'); 10 | t.is(md5Hex(['uni', Buffer.from('corn')]), '1abcb33beeb811dca15f0ac3e47b88d9'); 11 | }); 12 | 13 | test('ensure the Node.js and browser version match', t => { 14 | t.is(md5Hex('中文chinese'), md5HexBrowser('中文chinese')); 15 | }); 16 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | Create a MD5 hash with hex encoding. 3 | 4 | @param data - Prefer buffers as they're faster to hash, but strings can be useful for small things. 5 | 6 | Pass an array instead of concatenating strings and/or buffers. The output is the same, but arrays do not incur the overhead of concatenation. 7 | 8 | @example 9 | ``` 10 | import fs from 'node:fs'; 11 | import md5Hex from 'md5-hex'; 12 | 13 | const buffer = fs.readFileSync('unicorn.png'); 14 | 15 | md5Hex(buffer); 16 | //=> '1abcb33beeb811dca15f0ac3e47b88d9' 17 | ``` 18 | */ 19 | export default function md5Hex(data: Uint8Array | string | ReadonlyArray): string; 20 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "md5-hex", 3 | "version": "5.0.0", 4 | "description": "Create a MD5 hash with hex encoding", 5 | "license": "MIT", 6 | "repository": "sindresorhus/md5-hex", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": { 15 | "types": "./index.d.ts", 16 | "node": "./index.js", 17 | "default": "./browser.js" 18 | }, 19 | "sideEffects": false, 20 | "engines": { 21 | "node": ">=18" 22 | }, 23 | "scripts": { 24 | "test": "xo && ava && tsd" 25 | }, 26 | "files": [ 27 | "index.js", 28 | "index.d.ts", 29 | "browser.js" 30 | ], 31 | "keywords": [ 32 | "hash", 33 | "crypto", 34 | "md5", 35 | "hex", 36 | "buffer", 37 | "browser" 38 | ], 39 | "dependencies": { 40 | "blueimp-md5": "^2.19.0" 41 | }, 42 | "devDependencies": { 43 | "@types/node": "^20.8.7", 44 | "ava": "^5.3.1", 45 | "tsd": "^0.29.0", 46 | "xo": "^0.56.0" 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # md5-hex 2 | 3 | > Create a MD5 hash with hex encoding 4 | 5 | *Please don't use MD5 hashes for anything sensitive!* 6 | 7 | Works in the browser too, when used with a bundler (like Webpack, Rollup, Browserify). 8 | 9 | Checkout [`hasha`](https://github.com/sindresorhus/hasha) if you need something more flexible. 10 | 11 | ## Install 12 | 13 | ```sh 14 | npm install md5-hex 15 | ``` 16 | 17 | ## Usage 18 | 19 | ```js 20 | import fs from 'node:fs'; 21 | import md5Hex from 'md5-hex'; 22 | 23 | const buffer = fs.readFileSync('unicorn.png'); 24 | 25 | md5Hex(buffer); 26 | //=> '1abcb33beeb811dca15f0ac3e47b88d9' 27 | ``` 28 | 29 | ## API 30 | 31 | ### md5Hex(data) 32 | 33 | #### data 34 | 35 | Type: `Buffer | string | Array` 36 | 37 | Prefer buffers as they're faster to hash, but strings can be useful for small things. 38 | 39 | Pass an array instead of concatenating strings and/or buffers. The output is the same, but arrays do not incur the overhead of concatenation. 40 | 41 | ## Related 42 | 43 | - [crypto-hash](https://github.com/sindresorhus/crypto-hash) - Tiny hashing module that uses the native crypto API in Node.js and the browser 44 | - [hasha](https://github.com/sindresorhus/hasha) - Hashing made simple 45 | - [hash-object](https://github.com/sindresorhus/hash-object) - Get the hash of an object 46 | --------------------------------------------------------------------------------