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

sha-anything

v1.0.1

Published

A tiny TS utility to sha256 anything, including objects (with sorted keys option). Uses native crypto in browser & NodeJS.

Downloads

640

Readme

Sha anything 🔑

npm i sha-anything

Uses crypto-hash but adds support to hash anything, including objects, arrays, etc. with good TypeScript support. A small and simple integration.

Motivation

I often want to create a hash from an object like so:

sha256(someObject)

But you need to do a lot of extra work to make it work for objects.

sha-anything is a simple and small function that converts objects, arrays, etc. into hashable strings, and then pipes them through crypto-hash.

Usage

import { sha256 } from 'sha-anything'

await sha256({
  '001': { name: 'Bulbasaur', level: 10 },
  '004': { name: 'Charmander', level: 8 },
  '007': { name: 'Squirtle', level: 11 },
})
// '73e62b59429905357023c7afba82ea95dceffd6e4a0761519efdc389d0e51f0f'

Please note that a different object order, gives a different hash:

await sha256({
  '007': { name: 'Squirtle', level: 11 },
  '004': { name: 'Charmander', level: 8 },
  '001': { name: 'Bulbasaur', level: 10 },
})
// 'dcfa92cebc60af31015f058cb33a9ebe6b68d6e6fee066cb67a902b03dda5f0e'

Sort object keys

You can make sure you get the same hash by sorting your object keys. This feature is built in:

await sha256({
  '007': { name: 'Squirtle', level: 11 },
  '004': { name: 'Charmander', level: 8 },
  '001': { name: 'Bulbasaur', level: 10 },
}, { sort: true })
// '73e62b59429905357023c7afba82ea95dceffd6e4a0761519efdc389d0e51f0f'

Deep sort object keys

Setting deepSort: true will sort any object or array deeply.

await sha256({
  '001': { name: 'Bulbasaur', level: 10 },
  '004': { name: 'Charmander', level: 8 },
  '007': { name: 'Squirtle', level: 11 },
}, { deepSort: true })
// '64205611ef6f0d7ec4ebab1e3c0f84f1e4cb160af949cff3b32c0bdb0dbe7cb3'
await sha256({
  '007': { name: 'Squirtle', level: 11 },
  '004': { level: 8, name: 'Charmander' },
  '001': { name: 'Bulbasaur', level: 10 },
}, { deepSort: true })
// '64205611ef6f0d7ec4ebab1e3c0f84f1e4cb160af949cff3b32c0bdb0dbe7cb3'

Other types

await sha256('123') // 'a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3'
await sha256(123) // 'a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3'
await sha256(true) // 'b5bea41b6c623f7c09f1bf24dcae58ebab3c0cdd90ad966bc43a45b44867e12b'
await sha256('true') // 'b5bea41b6c623f7c09f1bf24dcae58ebab3c0cdd90ad966bc43a45b44867e12b'
await sha256(false) // 'fcbcf165908dd18a9e49f7ff27810176db8e9f63b4352213741664245224f8aa'
await sha256('false') // 'fcbcf165908dd18a9e49f7ff27810176db8e9f63b4352213741664245224f8aa'
await sha256(undefined) // 'eb045d78d273107348b0300c01d29b7552d622abbc6faf81b3ec55359aa9950c'
await sha256('undefined') // 'eb045d78d273107348b0300c01d29b7552d622abbc6faf81b3ec55359aa9950c'
await sha256(null) // '74234e98afe7498fb5daf1f36ac2d78acc339464f950703b8c019892f982b90b'
await sha256('null') // '74234e98afe7498fb5daf1f36ac2d78acc339464f950703b8c019892f982b90b'
await sha256(/\d/) // '841434ef0453514a69ca5c38926236f72f1496d64c7475feca3dd2e350f2fac6'
await sha256('/\\d/') // '841434ef0453514a69ca5c38926236f72f1496d64c7475feca3dd2e350f2fac6'
await sha256(/\D/) // 'a942b440234e1b6a646a583960bd8cbd15012f3acee2d5083552c7514a67de1e'
await sha256('/\\D/') // 'a942b440234e1b6a646a583960bd8cbd15012f3acee2d5083552c7514a67de1e'

The only thing you can't hash through sha256 is a symbol...

await sha256(Symbol('123')) // throws an error

Meet the family (more tiny utils with TS support)