Giter Site home page Giter Site logo

dchest / blake2s-js Goto Github PK

View Code? Open in Web Editor NEW
86.0 12.0 15.0 140 KB

BLAKE2s cryptographic hash function in JavaScript

Home Page: https://dchest.github.io/blake2s-js/

License: The Unlicense

JavaScript 99.72% HTML 0.28%
hash blake2s blake2 javascript

blake2s-js's Introduction

BLAKE2s implementation in JavaScript

BLAKE2 is a fast and secure cryptographic hash function.

This is a pure JavaScript public domain implementation of its BLAKE2s flavor (currently without tree mode support).

Build Status

This implementation is maintained, but will not receive new features. If you're looking for a full implementation of BLAKE2s, BLAKE2Xs and BLAKE2b, see my packages from StableLib: @stablelib/blake2s, @stablelib/blake2xs, and @stablelib/blake2b.

Installation

Via NPM:

$ npm install blake2s-js

or just download blake2s.min.js.

Usage

new BLAKE2s(digestLength, key)

new BLAKE2s(digestLength, config)

Creates a new instance of BLAKE2s hash with the given length of digest (default and maximum 32) and an optional secret key (a Uint8Array or Array of bytes) or config object in the following format:

{
    salt: // 8-byte Uint8Array or Array of bytes
    personalization: // 8-byte Uint8Array or Array of bytes
    key: // 0-32-byte Uint8Array or Array of bytes
}

All keys in config are optional.

.update(data[, offset, length])

Updates the hash with data (a Uint8Array or Array of bytes). starting at the given offset (optional, defaults to 0) and consuming the given length (optional, defaults to the length of data minus offset).

Returns this instance to enable method chaining.

.digest()

Returns a Uint8Array with the digest of consumed data. Further updates will throw error. Repeat calls of digest() will return the same digest.

.hexDigest()

Like digest(), but returns a hex-encoded string.

BLAKE2s.digestLength = 32

Maximum digest length.

BLAKE2s.blockLength = 64

Block size of the hash function.

BLAKE2s.keyLength = 32

Maximum key length.

BLAKE2s.personalizationLength = 8

Length of personalization parameter.

BLAKE2s.saltLength = 8

Length of salt parameter.

Example

var h = new BLAKE2s(32);
h.update(new Uint8Array([1,2,3]));
h.hexDigest();  // returns string with hex digest
h.digest();     // returns Uint8Array

// Keyed:
var key = new Uint8Array(BLAKE2s.keyLength);
window.crypto.getRandomValues(key);
var h = new BLAKE2s(32, key);
...

// Keyed and salted:
var key = new Uint8Array(BLAKE2s.keyLength);
var salt = new Uint8Array(BLAKE2s.saltLength);
window.crypto.getRandomValues(key);
window.crypto.getRandomValues(salt);
var h = new BLAKE2s(32, { key: key, salt: salt });
...

// Personalized:
var data = new Uint8Array([1, 2, 3]);
var pers1 = new Uint8Array([1, 0, 0, 0, 0, 0, 0, 0]);
var h1 = new BLAKE2s(32, { personalization: pers1 });
h1.update(data);

var pers2 = new Uint8Array([2, 0, 0, 0, 0, 0, 0, 0]);
var h2 = new BLAKE2s(32, { personalization: pers2 });
h2.update(data);

h1.hexDigest() !== h2.hexDigest() // true

blake2s-js's People

Contributors

dchest avatar ericyan avatar lostfictions 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

Watchers

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

blake2s-js's Issues

Update bug

Updating incrementally for short inputs produces inconsistent results:

var b = new BLAKE2s(32);
var msg = 'abc';
var msgArray = b.stringToUtf8Array(msg);
b.update(msgArray);
console.log(b.hexDigest()); // 508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982

var b = new BLAKE2s(32);
var msg = 'abc';
var msgArray = b.stringToUtf8Array(msg);
b.update(msgArray, 0, 1);
b.update(msgArray, 1, 2);
console.log(b.hexDigest()); // 19c3ebeed2ee90063cb5a8a4dd700ed7e5852dfc6108c84fac85888682a18f0e

Suggest fix - line 1283: change this.nx = length to this.nx += length;

How to use it in NodeJS?

Hi, i'm not understanding why i can't use it in NodeJS (an express app). The module gets exported as a promise? VSCode lets me jump to the .d.ts file...

// UnhandledPromiseRejectionWarning: Error: Not supported
var BLAKE2s = import('blake2s-js');

var gen = {
    hash: function(msg, key) {
        var hash;
        // TypeError: BLAKE2s is not a constructor
        hash = new BLAKE2s(32, key);
        hash.update(msg);
        return hash.digest();
    }
};

module.exports = gen;

Solution: use require() not import().
Leaving open because i still need to do some with TextEncoder/TextDecoder and/or Uint8Array...

Feature request: blake2xs

I noticed you implemented blake2xs in Go, any plans for a javascript implementation to compliment your fantastic javascript blake2s work?

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.