GithubHelp home page GithubHelp logo

es-sha-256's Introduction

NPM @simonbuchan/sha-256

SHA-256 core implementation in pure ECMAScript, for node or browser, in a functional-ish style, copyright free. Use however you want.

Has somewhat decent functional tests, if I do say so my self, and in my performance tests gets a little over 120MiB/s (hashing the same in-memory data), in comparison to Ubuntu's sha256sum getting over 300MiB/s on a (sparse) 600MiB file. I have not yet compared to the performance of the existing npm packages.

Will run into float64 precision errors past 2**50 byte files, but that would take over 100 days to run on my machine, so I'm not too concerned!

API

create(): Sha256State

Returns an opaque state object, representing an empty byte stream.

update(state: Sha256State, data: Uint8Array): void

Given a hash state object and some data, updates the state in-place

digest(state: Sha256State): string

Finishes the state, making it unusable, and formats the final hash as a 64 character hexadecimal digest (64 chars * 4 bits per hex char = 256 bits).

finish(state: Sha256State): Uint32Array

Finishes the state, making it unusable, and returns the raw final hash as an 8 element Uint32Array. Note that this is not a Uint8Array or Buffer, and the underlying ArrayBuffer stores the bytes in a machine-dependent endian order.

This is intended for lower-level usages.

Example

A CLI wrapper for this might look like:

const fs = require("fs");
const sha256 = require("@simonbuchan/sha-256");

async function main() {
  const state = sha256.create();
  for await (const chunk of fs.createReadStream(process.argv[2])) {
    sha256.update(state, chunk);
  }
  const hex = sha256.digest(state);
  console.log(hex);
}

main().catch((error) => {
  console.error(error);
  process.exit(1);
});

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.