GithubHelp home page GithubHelp logo

emn178 / js-sha512 Goto Github PK

View Code? Open in Web Editor NEW
215.0 10.0 75.0 151 KB

A simple SHA-512, SHA-384, SHA-512/224, SHA-512/256 hash functions for JavaScript supports UTF-8 encoding.

License: MIT License

JavaScript 97.71% HTML 2.29%

js-sha512's Introduction

js-sha512

Build Status Coverage Status CDNJS
NPM
A simple SHA-512, SHA-384, SHA-512/224, SHA-512/256 hash functions for JavaScript supports UTF-8 encoding.

Notice

  • v0.8.0+ will throw an error if try to update hash after finalize.

Demo

SHA512 Online
SHA384 Online
SHA512/256 Online
SHA512/224 Online

Download

Compress
Uncompress

Installation

You can also install js-sha512 by using Bower.

bower install js-sha512

For node.js, you can use this command to install:

npm install js-sha512

Usage

Node.js

If you use node.js, you should require the module first:

const { sha512, sha384, sha512_256, sha512_224 } = require('js-sha512');

TypeScript

If you use TypeScript, you can import like this:

import { sha512, sha384, sha512_256, sha512_224 } from 'js-sha512';

RequireJS

It supports AMD:

require(['your/path/sha512.js'], function(jsSha512) {
  const { sha512, sha384, sha512_256, sha512_224 } = jsSha512;
});

Classic Browser

If you use browser script directly, functions will be global:

sha512('Message to hash');
sha384('Message to hash');
sha512_256('Message to hash');
sha512_224('Message to hash');

Example

You could use like this:

sha512('Message to hash');
sha384('Message to hash');
sha512_256('Message to hash');
sha512_224('Message to hash');

// Support ArrayBuffer output
var arrayBuffer = sha512.arrayBuffer('Message to hash');

// Support Array output
var bytes = sha512.digest('Message to hash');
var bytes = sha512.array('Message to hash');

// update hash
var hash = sha512.update('Message to hash');
hash.update('Message2 to hash');
hash.hex();

// or to use create
var hash = sha512.create();
hash.update('Message to hash');
hash.hex();

// clone
var hash = sha512.update('Message to hash');
var hash2 = hash.clone();
hash2.hex();
hash.update('Message2 to hash');
hash.hex();

// HMAC
sha512.hmac('key', 'Message to hash');
sha384.hmac('key', 'Message to hash');

var hash = sha512.hmac.create('key');
hash.update('Message to hash');
hash.hex();

var hash2 = sha512.hmac.update('key', 'Message to hash');
hash2.update('Message2 to hash');
hash2.array();

sha512(''); // cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e
sha512('The quick brown fox jumps over the lazy dog'); // 07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6
sha512('The quick brown fox jumps over the lazy dog.'); // 91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed
sha384(''); // 38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b
sha384('The quick brown fox jumps over the lazy dog'); // ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1
sha384('The quick brown fox jumps over the lazy dog.'); // ed892481d8272ca6df370bf706e4d7bc1b5739fa2177aae6c50e946678718fc67a7af2819a021c2fc34e91bdb63409d7
sha512_256(''); // c672b8d1ef56ed28ab87c3622c5114069bdd3ad7b8f9737498d0c01ecef0967a
sha512_256('The quick brown fox jumps over the lazy dog'); // dd9d67b371519c339ed8dbd25af90e976a1eeefd4ad3d889005e532fc5bef04d
sha512_256('The quick brown fox jumps over the lazy dog.'); // 1546741840f8a492b959d9b8b2344b9b0eb51b004bba35c0aebaac86d45264c3
sha512_224(''); // 6ed0dd02806fa89e25de060c19d3ac86cabb87d6a0ddd05c333b84f4
sha512_224('The quick brown fox jumps over the lazy dog'); // 944cd2847fb54558d4775db0485a50003111c8e5daa63fe722c6aa37
sha512_224('The quick brown fox jumps over the lazy dog.'); // 6d6a9279495ec4061769752e7ff9c68b6b0b3c5a281b7917ce0572de

// It also supports UTF-8 encoding
sha512('中文'); // 8b88efc2ebbcbdad5ac2d65af05bec57bda25e71fd5fb25bbd892057a2755fbd05d8d8491cb2946febd5b0f124ffdfbaecf7e34946353c4f1b5ab29545895468
sha384('中文'); // 93422ceb8291a69b22f02dc1114c39a287493ad525dcebc77e4019a44eaee2633a85d0f29cd298ee6799048c33a4be0c
sha512_256('中文'); // b6dab29c16ec35ab34a5d92ff135b58de96741dda78b1009a2181cf8b45d2f72
sha512_224('中文'); // 0f46a0ae7f226517dd66ece0ce1efa29ffb7ced05ac4566fdcaed188

// Different output
sha512(''); // cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e
sha512.hex(''); // cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e
sha512.array(''); // [207, 131, 225, 53, 126, 239, 184, 189, 241, 84, 40, 80, 214, 109, 128, 7, 214, 32, 228, 5, 11, 87, 21, 220, 131, 244, 169, 33, 211, 108, 233, 206, 71, 208, 209, 60, 93, 133, 242, 176, 255, 131, 24, 210, 135, 126, 236, 47, 99, 185, 49, 189, 71, 65, 122, 129, 165, 56, 50, 122, 249, 39, 218, 62]
sha512.digest(''); // [207, 131, 225, 53, 126, 239, 184, 189, 241, 84, 40, 80, 214, 109, 128, 7, 214, 32, 228, 5, 11, 87, 21, 220, 131, 244, 169, 33, 211, 108, 233, 206, 71, 208, 209, 60, 93, 133, 242, 176, 255, 131, 24, 210, 135, 126, 236, 47, 99, 185, 49, 189, 71, 65, 122, 129, 165, 56, 50, 122, 249, 39, 218, 62]
sha512.arrayBuffer(''); // ArrayBuffer

// HMAC
sha512.hmac.hex('key', 'Message to hash');
sha512.hmac.array('key', 'Message to hash');

License

The project is released under the MIT license.

Contact

The project's website is located at https://github.com/emn178/js-sha512
Author: Chen, Yi-Cyuan ([email protected])

js-sha512's People

Contributors

emn178 avatar nolsherry avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

js-sha512's Issues

Only for Minified file

This is only a format issued
For easy convert the original file to minified version.
Manually we can delete all the new lines and unnecessary spaces.
But in the original source at version 0.3.0 we need delete the comment "// 512" or replace for "/* 512 */"

SHA512 HMAC Support

HMAC Support like js-sha256

// HMAC
sha512.hmac('key', 'Message to hash');

var hash = sha512.hmac.create('key');
hash.update('Message to hash');
hash.hex();

WARNING in login.component.ts depends on 'js-sha512'. CommonJS or AMD dependencies can cause optimization bailouts. For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies

I'm using js-sha512 in Angular 10, with ng build I get the following error:

WARNING in D:\Sources\Karnama\Apps\Karnama-Angular\src\app\account\login\login.component.ts depends on 'js-sha512'. CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies

Could you please check it and how to resolve it?

Report update progress

Feature request

  1. Take a callback in the update function/constructor. Call it once in a few blocks with some format of progress on the current input.
  2. If this has a measurable performance/memory use impact, make this an option on the constructor, so that those who don't need it don't get affected.

Please provide an ECMAScript module

CommonJS modules cannot be optimized with webpack, for example, and the bundle size of the application is potentially larger.

As of Angular 10, a warning is emitted when building an app that imports js-sha512.

minimum browser version

Does somebody have any information about the minimum browser version please? Especially chrome I am interested in.

Incomplete Typescript types

As seen here, the code already supports using an ArrayBuffer as input for the key when instanciating HmacSha512, but the types state that it only accepts a string as the key (see below) - the types should be updated to reflect that it can accept other types as well.

js-sha512/index.d.ts

Lines 37 to 51 in b6c4431

interface Hmac {
/**
* Computes a Hash-based message authentication code (HMAC) using a secret key
*
* @param secretKey The Secret Key
* @param message The message you want to hash.
*/
(secretKey: string, message: Message): string;
/**
* Create a hash object using a secret key.
*
* @param secretKey The Secret Key
*/
create(secretKey: string): Hasher;

Ref :

js-sha512/src/sha512.js

Lines 819 to 859 in b6c4431

function HmacSha512(key, bits, sharedMemory) {
var notString, type = typeof key;
if (type !== 'string') {
if (type === 'object') {
if (key === null) {
throw new Error(INPUT_ERROR);
} else if (ARRAY_BUFFER && key.constructor === ArrayBuffer) {
key = new Uint8Array(key);
} else if (!Array.isArray(key)) {
if (!ARRAY_BUFFER || !ArrayBuffer.isView(key)) {
throw new Error(INPUT_ERROR);
}
}
} else {
throw new Error(INPUT_ERROR);
}
notString = true;
}
var length = key.length;
if (!notString) {
var bytes = [], length = key.length, index = 0, code;
for (var i = 0; i < length; ++i) {
code = key.charCodeAt(i);
if (code < 0x80) {
bytes[index++] = code;
} else if (code < 0x800) {
bytes[index++] = (0xc0 | (code >> 6));
bytes[index++] = (0x80 | (code & 0x3f));
} else if (code < 0xd800 || code >= 0xe000) {
bytes[index++] = (0xe0 | (code >> 12));
bytes[index++] = (0x80 | ((code >> 6) & 0x3f));
bytes[index++] = (0x80 | (code & 0x3f));
} else {
code = 0x10000 + (((code & 0x3ff) << 10) | (key.charCodeAt(++i) & 0x3ff));
bytes[index++] = (0xf0 | (code >> 18));
bytes[index++] = (0x80 | ((code >> 12) & 0x3f));
bytes[index++] = (0x80 | ((code >> 6) & 0x3f));
bytes[index++] = (0x80 | (code & 0x3f));
}
}
key = bytes;

How to unhash?

Is there a way to get the original message after it is hashed?
ex:
var hashed = sha512.hmac('key', 'Message to hash'); var unhashed = sha512.unhmac('key', '840fd9766d83b57f06eead2bba0d4632b7dfad8f3645d9e4c70cce68c309f996f45d6e3d904f2acda0f2e84da8d8970429624622b9001cddb9f5f7c5a6506b0a');

Allow continuing a hash after getting a readout

Feature request

  1. Make the .finalize() not mutate the object, so that you can .arrayBuffer() to get a digest but then continue .update()-ing the same hash.
  2. If this has a measurable performance/memory use impact, make this an option on the constructor or the finalize function, so that those who don't need it don't get affected.
  3. Cache the finalization and invalidate it only when updating, so that h.hex(); h.arrayBuffer(); h.hex(); h.arrayBuffer(); h.update(); only carries out the finalization process once.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.