Skip to content

A Javascript implementation of SipHash-2-4

License

Notifications You must be signed in to change notification settings

PostHog/siphash-js

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

71 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

siphash.js

A pure Javascript implementation of SipHash

SipHash is a family of pseudorandom functions optimized for short inputs. Target applications include network traffic authentication and hash-table lookups protected against hash-flooding denial-of-service attacks. SipHash has well-defined security goals and competitive performance.

This package also includes an implementation of SipHash128.

Fork notes

This package is a fork of https://github.com/jedisct1/siphash-js

Installation

Server-side installation (nodejs):

$ npm install @posthog/siphash

Usage

import * as siphash from 'siphash';

const key = siphash.string16_to_key("This is the key!");
const message = "Short test message";
const hash_hex = siphash.hash_hex(key, message);

A key is an array of 4 integers, and each of them will be clamped to 32 bits in order to build a 128-bit key. For a random key, just generate 4 random integers instead of calling string16_to_key().

import * as siphash from 'siphash'

const key = [0xdeadbeef, 0xcafebabe, 0x8badf00d, 0x1badb002];
const message = "Short test message";
const hash_hex = siphash.hash_hex(key, message);

The 64-bit hash can also be obtained as two 32-bit values with hash(key, message):

import * as siphash from 'siphash'

const key = [0xdeadbeef, 0xcafebabe, 0x8badf00d, 0x1badb002];
const message = "Short test message";
const hash = siphash.hash(key, message);
const hash_msb = hash.h;
const hash_lsb = hash.l;

A 53-bit unsigned integer can be obtained with hash_uint(key, message):

import * as siphash from 'siphash'

const key = siphash.string16_to_key("0123456789ABCDEF");
const message = "Short test message";
const index = siphash.hash_uint(key, message);

SipHash-double

import * as siphash from 'siphash/lib/siphash-double';

const key = siphash.string16_to_key("This is the key!");
const message = "Short test message";
const hash_hex = siphash.hash_hex(key, message);

About

A Javascript implementation of SipHash-2-4

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 81.0%
  • TypeScript 19.0%