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

cryptopan

v0.3.1

Published

Node.js implementation of the Crypto-PAn IP address pseudonymisation algorithm

Downloads

11

Readme

cryptopan

npm

Node.js implementation of the Crypto-PAn scheme. Cryptographically pseudonymises IP addresses in a way that subnets can still be compared (in addition to exact addresses).

Like the reference implementation and most others, it uses AES-128-ECB as the pseudorandom function (PRF).

Installation

npm install cryptopan
# or
yarn add cryptopan

Usage

const { Buffer } = require('buffer');
const { CryptoPAn } = require('cryptopan');

const cryptopan = new CryptoPAn(SECRET_KEY);

const ipv4 = Buffer.from([192,0,2,1]);  // <Buffer c0 00 02 01>
const ipv6 = Buffer.from('20010db8000000000000000000000001', 'hex');  // <Buffer 20 01 0d b8 00 ... 01>

cryptopan.pseudonymiseIP(ipv4);  // e.g. <Buffer 3c 0c fe 2e>
cryptopan.pseudonymiseIP(ipv6);  // e.g. <Buffer a0 01 3d bc 26 30 0e 00 e2 7f 5f 84 8f 07 3e e6>

Notes

In order to keep things minimal, only Buffer (or plain Uint8Array) objects are accepted as input and produced as output. If you need to convert to/from string representations of IP addresses, it's recommended to use an existing library with that capability. For example, using ipaddr.js:

const bytes = ipaddr.parse('2001:db8::1').toByteArray();  // standard array of byte values
const inputBuffer = Buffer.from(bytes);
const outputBuffer = cryptopan.pseudonymiseIP(inputBuffer);
const pseudonymisedIP = ipaddr.fromByteArray(outputBuffer).toString();

Outputs from different CryptoPAn instances are only comparable when the same secret key is used to create them. To generate an appropriate 32-byte key (which should be stored somewhere safe) you can run this from the terminal:

node -p 'crypto.randomBytes(32).toString("hex")'

You can use Buffer.from(keyString, 'hex') to convert it back to a buffer so it can be used with the CryptoPAn constructor.

API

Constructor

new CryptoPAn(key)

  • key: Buffer | Uint8Array: 32-byte (256-bit) secret key. The first half is used as the key for the AES-128-ECB cipher. The second half is used to derive the padding.

Pseudonymisation

The primary functionality: take an IP address and generate a pseudonym for it.

In all cases ip must be a Buffer or plain Uint8Array. The choice will also determine the return type.

cryptopan.pseudonymiseIP(ip)

  • ip: Bytes representing an IPv4 or IPv6 address (network/big-endian order)

cryptopan.pseudonymiseIPv4(ip)

  • ip: Bytes representing an IPv4 address

Throws if an IPv6 address buffer is provided.

cryptopan.pseudonymiseIPv6(ip)

  • ip: Bytes representing an IPv6 address

Throws if an IPv4 address buffer is provided.

De-pseudonymisation/reversal

Take an IP address pseudonym (that has been generated using the same key) and decrypt it to return the original.

In all cases pseudonymisedIP must be a Buffer or plain Uint8Array. The choice will also determine the return type.

cryptopan.depseudonymiseIP(pseudonymisedIP)

cryptopan.depseudonymiseIPv4(pseudonymisedIP)

cryptopan.depseudonymiseIPv6(pseudonymisedIP)

Features that may be supported in the future

  • Option to use a suggested technique to increase the randomness of outputs^1