npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@root/sha256

v1.0.0

Published

A brower, node, and bun-compatible synchronous sha256 implementation using TypedArray

Downloads

6

Readme

@root/sha256

A brower, node, and bun-compatible synchronous sha256 implementation using typed arrays

For Browsers

<script src="https://unpkg.com/@root/sha256@1/sha256.js"></script>
let Sha256 = window.Sha56;

let msg = "Hello, World!";
let encoder = new TextEncoder();
let bytes = encoder.encode(msg);

let hashBytes = Sha256.sha256(bytes);
console.log(hashBytes);
let hex = bytesToHex(hashBytes);
console.log(hex);

function bytesToHex(bytes) {
  let hexes = [];

  for (let b of bytes) {
    let h = b.toString(16);
    h = h.padStart(2, "0");
    hexes.push(b);
  }

  let hex = hexes.join("");
  return hex;
}

For Node, Bun, and Bundlers

npm install --save @root/sha256@1
let Sha256 = require("@root/sha256");

let msg = "Hello, World!";
let encoder = new TextEncoder();
let bytes = encoder.encode(msg);

let hashBytes = Sha256.sha256(bytes);
console.log(hashBytes);

let buf = Buffer.from(hashBytes);
let hex = buf.toString("hex");
console.log(hex);

Why?

  • WebCrypto's SHA-256 is asynchronous, which means:
    • it's very slow (context switches on each call)
    • it colors all the functions (sha256 is cpu-bound and typically sync)
  • Other popular implementations have bespoke psuedo-buffers (not TypedArrays)
  • Refactoring other implementations is tedious and error-prone

References

Since hundreds (if not thousands) of correct implementations exist, this was primarily generated via prompt-engineering rather than ported by hand (which is error-prone due to the differences in endianness, int width and sign, etc). However, the code has been manually compared to other implementations for correctness, and the results have been verified through over a thousand tests.

To compare to other known-working implementations, consider:

  • https://sha256algorithm.com/
    • https://github.com/dmarman/sha256algorithm/blob/main/src/classes/sha.js
  • https://brillout.github.io/test-javascript-hash-implementations/
  • https://github.com/dchest/fast-sha256-js/blob/master/src/sha256.ts
  • https://github.com/digitalbazaar/forge/blob/main/lib/sha256.js
  • https://gist.github.com/bryanchow/1649353
  • https://github.com/brix/crypto-js/blob/develop/src/sha256.js

License

Copyright 2024 AJ ONeal (MPL-2.0 License)