├── .npmignore ├── .travis.yml ├── LICENSE ├── README.md ├── browser.js ├── index.js ├── package.json └── test ├── basic.js └── browser.js /.npmignore: -------------------------------------------------------------------------------- 1 | .travis.yml 2 | test/ 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - lts/* 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Feross Aboukhadijeh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # simple-sha256 [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url] 2 | 3 | [travis-image]: https://img.shields.io/travis/feross/simple-sha256/master.svg 4 | [travis-url]: https://travis-ci.org/feross/simple-sha256 5 | [npm-image]: https://img.shields.io/npm/v/simple-sha256.svg 6 | [npm-url]: https://npmjs.org/package/simple-sha256 7 | [downloads-image]: https://img.shields.io/npm/dm/simple-sha256.svg 8 | [downloads-url]: https://npmjs.org/package/simple-sha256 9 | [standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg 10 | [standard-url]: https://standardjs.com 11 | 12 | ### Generate SHA-256 hashes (in Node and the Browser) 13 | 14 | In Node.js, this package uses [`crypto.createHash()`](https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm_options). In the browser, it uses [`crypto.subtle.digest()`](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest). 15 | 16 | ### install 17 | 18 | ``` 19 | npm install simple-sha256 20 | ``` 21 | 22 | ### usage 23 | 24 | Async (returns a `Promise`): 25 | 26 | ```js 27 | const sha256 = require('simple-sha256') 28 | 29 | init() 30 | 31 | async function init () { 32 | const hash = await sha256('hey there') 33 | console.log(hash) 34 | // 74ef874a9fa69a86e091ea6dc2668047d7e102d518bebed19f8a3958f664e3da 35 | } 36 | ``` 37 | 38 | Sync: 39 | 40 | ```js 41 | const sha256 = require('simple-sha256') 42 | console.log(sha256.sync('hey there')) 43 | // 74ef874a9fa69a86e091ea6dc2668047d7e102d518bebed19f8a3958f664e3da 44 | ``` 45 | 46 | ### see also 47 | 48 | - [simple-sha1](https://www.npmjs.com/package/simple-sha1) 49 | 50 | ### license 51 | 52 | MIT. Copyright (c) [Feross Aboukhadijeh](https://feross.org). 53 | -------------------------------------------------------------------------------- /browser.js: -------------------------------------------------------------------------------- 1 | module.exports = sha256 2 | module.exports.sync = sha256sync 3 | 4 | const crypto = globalThis.crypto || globalThis.msCrypto 5 | const subtle = crypto.subtle || crypto.webkitSubtle || (crypto.webcrypto && crypto.webcrypto.subtle) 6 | 7 | function sha256sync (buf) { 8 | throw new Error('No support for sha256.sync() in the browser, use sha256()') 9 | } 10 | 11 | async function sha256 (buf) { 12 | if (typeof buf === 'string') buf = strToBuf(buf) 13 | 14 | // Browsers throw if they lack support for an algorithm. 15 | // Promise will be rejected on non-secure origins. (http://goo.gl/lq4gCo) 16 | const hash = await subtle.digest({ name: 'sha-256' }, buf) 17 | return hex(new Uint8Array(hash)) 18 | } 19 | 20 | function strToBuf (str) { 21 | const len = str.length 22 | const buf = new Uint8Array(len) 23 | for (let i = 0; i < len; i++) { 24 | buf[i] = str.charCodeAt(i) 25 | } 26 | return buf 27 | } 28 | 29 | function hex (buf) { 30 | const len = buf.length 31 | const chars = [] 32 | for (let i = 0; i < len; i++) { 33 | const byte = buf[i] 34 | chars.push((byte >>> 4).toString(16)) 35 | chars.push((byte & 0x0f).toString(16)) 36 | } 37 | return chars.join('') 38 | } 39 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! simple-sha256. MIT License. Feross Aboukhadijeh */ 2 | module.exports = sha256 3 | module.exports.sync = sha256sync 4 | 5 | const crypto = require('crypto') 6 | 7 | async function sha256 (buf) { 8 | return sha256sync(buf) 9 | } 10 | 11 | function sha256sync (buf) { 12 | return crypto.createHash('sha256') 13 | .update(buf) 14 | .digest('hex') 15 | } 16 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "simple-sha256", 3 | "description": "Generate SHA-256 hashes (in Node and the Browser)", 4 | "version": "1.1.0", 5 | "author": { 6 | "name": "Feross Aboukhadijeh", 7 | "email": "feross@feross.org", 8 | "url": "https://feross.org" 9 | }, 10 | "browser": "./browser.js", 11 | "bugs": { 12 | "url": "https://github.com/feross/simple-sha256/issues" 13 | }, 14 | "dependencies": {}, 15 | "devDependencies": { 16 | "airtap": "^3.0.0", 17 | "standard": "*", 18 | "tape": "^5.0.1" 19 | }, 20 | "homepage": "https://github.com/feross/simple-sha256", 21 | "keywords": [ 22 | "browser", 23 | "browserify", 24 | "node", 25 | "sha256", 26 | "hash", 27 | "sha-256" 28 | ], 29 | "license": "MIT", 30 | "main": "index.js", 31 | "repository": { 32 | "type": "git", 33 | "url": "git://github.com/feross/simple-sha256.git" 34 | }, 35 | "scripts": { 36 | "test": "standard && tape test/*.js", 37 | "test-browser-local": "airtap --local -- test/*.js" 38 | }, 39 | "funding": [ 40 | { 41 | "type": "github", 42 | "url": "https://github.com/sponsors/feross" 43 | }, 44 | { 45 | "type": "patreon", 46 | "url": "https://www.patreon.com/feross" 47 | }, 48 | { 49 | "type": "consulting", 50 | "url": "https://feross.org/support" 51 | } 52 | ] 53 | } 54 | -------------------------------------------------------------------------------- /test/basic.js: -------------------------------------------------------------------------------- 1 | const sha256 = require('../') 2 | const test = require('tape') 3 | 4 | const TEXT = 'hey there' 5 | const HASH = '74ef874a9fa69a86e091ea6dc2668047d7e102d518bebed19f8a3958f664e3da' 6 | 7 | test('sha256', function (t) { 8 | t.plan(2) 9 | 10 | // buffer 11 | sha256(Buffer.from(TEXT)).then(hash => { 12 | t.equal(hash, HASH) 13 | }) 14 | // string 15 | sha256(TEXT).then(hash => { 16 | t.equal(hash, HASH) 17 | }) 18 | }) 19 | 20 | test('sha256.sync', function (t) { 21 | if (process.browser) { 22 | t.pass('skipping in browser') 23 | return t.end() 24 | } 25 | 26 | // buffer 27 | t.equal(sha256.sync(Buffer.from(TEXT)), HASH) 28 | // string 29 | t.equal(sha256.sync(TEXT), HASH) 30 | 31 | t.end() 32 | }) 33 | -------------------------------------------------------------------------------- /test/browser.js: -------------------------------------------------------------------------------- 1 | // These tests are not run on node versions prior to 14, as they lack webcrypto. 2 | 3 | const crypto = require('crypto') 4 | 5 | if (!globalThis.crypto) { 6 | globalThis.crypto = crypto 7 | } 8 | 9 | const sha256 = require('../browser') 10 | const test = require('tape') 11 | 12 | if (globalThis.crypto.webcrypto) { 13 | const TEXT = 'hey there' 14 | const HASH = '74ef874a9fa69a86e091ea6dc2668047d7e102d518bebed19f8a3958f664e3da' 15 | 16 | test('sha256', function (t) { 17 | t.plan(2) 18 | 19 | // buffer 20 | sha256(Buffer.from(TEXT)).then(hash => { 21 | t.equal(hash, HASH) 22 | }) 23 | // string 24 | sha256(TEXT).then(hash => { 25 | t.equal(hash, HASH) 26 | }) 27 | }) 28 | 29 | test('sha256.sync', function (t) { 30 | t.throws(function () { 31 | sha256.sync(Buffer.from(TEXT)) 32 | }, /No support for sha256\.sync\(\) in the browser, use sha256\(\)/) 33 | 34 | t.end() 35 | }) 36 | } 37 | --------------------------------------------------------------------------------