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

ip-anonymize

v0.1.0

Published

Anonymize IP addresses, works with IPv4 and IPv6

Downloads

53,338

Readme

ip-anonymize

Build Status codecov dependencies Status npm npm

Truncate IPv4 and IPv6 address binary and format back as an IP address.

  • Supports IPv4
  • Supports IPv6
    • Supports x:x:x:x:x:x:x:x format
    • Supports x:x:x:x:x:x:d.d.d.d format
    • Automatically compresses zeros optimally
  • Choose number of bits to keep
anonymize('192.168.42.1') // -> '192.168.42.0'
anonymize('192.168.42.1', 16) // -> '192.168.0.0'
anonymize('::ffff:192.168.42.1', 16) // -> '::ffff:192.168.0.0'
anonymize('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', 16, 16) // -> 'ffff::'

This package converts an IP address to binary and keeps a specified number of bits on the left while setting the remaining bits to zero. It then converts the binary back into an IP address string and returns it. For example, if the binary was 10101010 and we wanted to keep the first four bits (4-bit mask), the binary would become 10100000.

Note: Although this technique adds some uncertainty about the actual IP address, using this package is not sufficient to anonymize users in all cases, such as if the traffic from one user comes from one large IP block or if other information is stored along with the truncated IP address. This package is meant to simply help with truncating and formatting IP addresses and makes no claim that this technique is the best choice for any particular use case. Take extra care when storing IP addresses along with other potentially identifying information.

Getting Started

$ npm install ip-anonymize --save # Using npm
$ yarn add ip-anonymize # OR use yarn

Usage

const anonymize = require('ip-anonymize')

const ipv4 = '192.168.1.16'
anonymize(ipv4) // '192.168.1.0'

// Use 16-bit mask
anonymize(ipv4, 8) // '192.0.0.0'

const ipv6 = 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'
anonymize(ipv6) // 'ffff:ff00::'

// Use 16-bit mask (first number is for IPv4, second for IPv6)
anonymize(ipv6, 16, 16) // 'ffff::'

// IPv4-compatible IPv6 and IPv4-mapped IPv6 use the IPv4 mask on the IPv4 part
const ipv4Compatible = '::192.168.0.1'
anonymize(ipv4Compatible, 16, 16) // '::192.168.0.0'
const ipv4Mapped = '::ffff:192.168.0.1'
anonymize(ipv4Mapped, 16, 16) // '::ffff:192.168.0.0'

// Returns null if IP address is invalid
anonymize('not an ip', 16, 16) // null

API Documentation

const anonymize = require('ip-anonymize')
anonymize(ip [, v4MaskLength, v6MaskLength])
  • ip: String, The IP address to anonymize
  • v4MaskLength: Number, Number of bits to keep at the beginning of an IPv4 address or IPv4 part of IPv4-compatible/mapped IPv6 address (default: 24)
  • v6MaskLength: Number, Number of bits to keep at the beginning of an IPv6 address (default: 24)

Returns null if the IP address is invalid.

References

  1. IPv6 representation: RFC3513
  2. IPv6 validator
  3. IPv4-compatible and IPv4-mapped IPv6 addresses