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

scrypt-hashcash

v0.0.2

Published

Scrypt hashcash implementation for node & browsers.

Downloads

6

Readme

Scrypt hashcash implementation for node & browsers.

npm i scrypt-hashcash
sh = require("scrypt-hashcash");
<script src="script-hashcash.min.js"></script>

API

sh.pow(text, target, [noncefunction, callback])

Generate a proof-of-work token over some text for a given target difficulty.

  • text is the material to be hashed over.
  • target is the difficulty vector to try to outperform (use sh.target(bits) to generate a target vector for a particular difficulty of bits zero-bits.
  • noncefunction is an optional callback function to return nonces which takes a single argument i being the number of iterations so far.
  • callback is an optional callback for when the promise API is not used.
sh.pow("some text", sh.target(4)).then(function(pow) {
  console.log("Nonce found:", sh.toHex(pow.nonce));
  console.log("Resulting hash:", sh.toHex(pow.hash));
  console.log("Iterations it took:", pow.iterations);
});

sh.verify(text, nonce, target, [callback])

Verify some previously found nonce hashes to a lower value than the target difficulty specified.

  • text is the material which was hashed to find the nonce.
  • nonce is the nonce which the client claims to have found to beat the target.
  • target is the difficult vector to be verfied has having been exceeded.
  • callback is an optional callback for when the promise API is not used.
sh.verify(text, nonce, target).then(function(v) {
  if (v.verified) {
    console.log("verification passed with hash " + sh.toHex(v.hash));
  } else {
    console.log("verification failed for this nonce");
  }
});

sh.target(bits)

Generate a target vector for a particular number of zero-bits.

  • bits is the number of zero-bits of difficulty required.
> sh.target(4);
Uint8Array [ 15, 255, 255, 255, 255, 255, 255, 255 ]

sh.difficulty(hashrate, time)

Calculate how many bits of difficulty are required on average to run at a particular hashrate for a particular time.

  • hashrate is the hashrate expected.
  • time is the amount of time (e.g. seconds).
> s.difficulty(100, 10)
9

sh.measure()

Measure the number of hashes per second the current device can perform.

  • iterations is an optional number of iterations to carry out (defaults to 50).
s.measure().then(function(d) {
  console.log(d + " hashes per second");
})

sh.toHex(h)

Convenience function to convert an array to a hex string representation.

sh.fromHex(h)

Convenience function to convert a hex string to a Uint8Array.

sh.config

The scrypt configuration being used. Parameters include dkLen, N, r, and p. See the Scrypt Wikipedia page for a summary of these variables and this blog post for a treatment of how to tune them for different uses.

Command line usage

You can use scrypt-hashcash from the command line.

For example, to find a collision of 4 bits against the test vector:

$ scrypt-hashcash 0:030626:[email protected] 4
0:030626:[email protected]:8043dfc5850f9525

Then to verify the nonce that was found and get the hash:

$ scrypt-hashcash --verify 0:030626:[email protected]:213dc824e7eed707 4
Verified: 0120e170dead0aab

Test a bad nonce:

$ scrypt-hashcash --verify 0:030626:[email protected]:213dc824e7eed709 4
Verification failed! Hash does not have sufficient zero bits for this nonce.
$ echo $?
2

Measure the hashes per second of the current device:

$ scrypt-hashcash --measure
79.18 hashes per second