Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Commit

Permalink
Merge 4b79df0 into 15603b8
Browse files Browse the repository at this point in the history
  • Loading branch information
PizzaBrandon committed Nov 23, 2016
2 parents 15603b8 + 4b79df0 commit 80717f0
Show file tree
Hide file tree
Showing 13 changed files with 321 additions and 369 deletions.
6 changes: 5 additions & 1 deletion .eslintrc
Expand Up @@ -3,7 +3,11 @@
"node": true
},

"extends": "eslint:recommended",
"parser": "typescript-eslint-parser",
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},

"rules": {
"no-console": 2,
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -7,6 +7,7 @@ artifacts/
docs/
.nyc_output/
logs/
dist/

*.log
*.tap
Expand Down
7 changes: 6 additions & 1 deletion .npmignore
@@ -1,2 +1,7 @@
circle.yml
.travis.yml
test/
src/
coverage/
tsconfig.json
tsconfig.build.json
artifacts/
12 changes: 10 additions & 2 deletions .travis.yml
Expand Up @@ -2,8 +2,16 @@ language: node_js

node_js:
- "4"
- "0.12"
- "0.10"
- "6"
- "node"

install:
- npm install

script:
- npm run test-ci

after_success:
- './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'

sudo: false
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -2,7 +2,7 @@

A fast two-way encryption module to generate unique, random-appearing, non-sequential strings from integers. This is a great way to encode database primary keys before presenting them to the user.

[![Build Status](https://travis-ci.org/zefferus/concealer.svg?branch=master)](https://travis-ci.org/zefferus/concealer)![Current Version](https://img.shields.io/npm/v/concealer.svg)
[![Build Status](https://travis-ci.org/zefferus/concealer.svg?branch=master)](https://travis-ci.org/zefferus/concealer) [![Coverage Status](https://coveralls.io/repos/github/zefferus/concealer/badge.svg?branch=master)](https://coveralls.io/github/zefferus/concealer?branch=master) [![Current Version](https://img.shields.io/npm/v/concealer.svg)](https://www.npmjs.com/package/concealer)

Development on **Concealer** is sponsored by [Sparo Labs](http://www.sparolabs.com/).

Expand Down
10 changes: 5 additions & 5 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 25 additions & 10 deletions package.json
@@ -1,16 +1,23 @@
{
"name": "concealer",
"version": "1.0.0",
"version": "2.0.0",
"description": "A primary key encoding utility",
"main": "src/concealer.js",
"main": "dist/concealer.js",
"engines": {
"node": ">=0.10.0"
"node": ">=4.0"
},
"scripts": {
"lint": "node_modules/.bin/eslint src",
"test": "node_modules/.bin/tap --cov --reporter=spec test/**/*.test.js",
"precoverage": "node_modules/.bin/rimraf coverage",
"coverage": "node_modules/.bin/tap --coverage-report=lcov"
"lint": "eslint src/**/*.ts",
"pretest": "rimraf dist && tsc",
"test": "nyc --cache ava --tap | tap-nyan",
"posttest": "nyc report --check-coverage --statements=90",
"coverage:report": "nyc report --reporter=lcov && opener ./coverage/lcov-report/index.html",
"pretest-ci": "rimraf dist && tsc",
"test-ci": "nyc --cache ava --verbose",
"posttest-ci": "nyc report --check-coverage --statements=90",
"prebuild": "npm run lint && npm run test && rimraf dist",
"build": "tsc -p tsconfig.build.json --sourceMap false -d",
"prepackage": "npm run build"
},
"repository": {
"type": "git",
Expand All @@ -29,13 +36,21 @@
"url": "https://github.com/zefferus/concealer/issues"
},
"homepage": "https://github.com/zefferus/concealer#readme",
"typings": "dist/concealer.d.ts",
"devDependencies": {
"eslint": "^2.4.0",
"@types/node": "^6.0.51",
"ava": "^0.17.0",
"coveralls": "^2.11.15",
"eslint": "^3.10.2",
"nyc": "^10.0.0",
"opener": "^1.4.2",
"rimraf": "^2.5.2",
"tap": "^5.7.0"
"tap-nyan": "^1.1.0",
"typescript": "^2.1.1",
"typescript-eslint-parser": "^1.0.0"
},
"dependencies": {
"hashids": "^1.0.2",
"hashids": "^1.1.1",
"skip32": "^1.2.1"
}
}
105 changes: 0 additions & 105 deletions src/concealer.js

This file was deleted.

60 changes: 60 additions & 0 deletions src/concealer.ts
@@ -0,0 +1,60 @@
var Skip32: any = require('skip32').Skip32;
var Hashids: any = require('hashids');

// Public methods

export class Concealer {

private skip32: any;
private hashids: any;

constructor(private secretKey: number[], private salt: string,
private minLength = 1, private customAlphabet?: string) {

if (!secretKey) {
throw new Error('Secret Key must be provided');
}
if (!salt) {
throw new Error('Salt must be provided');
}
if (!Array.isArray(secretKey) || !secretKey.length || !secretKey.every(isByte)) {
throw new TypeError('Secret Key must be an Array of bytes represented by integers');
}
if (minLength !== undefined && !Number.isInteger(minLength)) {
throw new TypeError('Min Length must be an integer, if provided');
}

this.skip32 = new Skip32(secretKey);
this.hashids = new Hashids(salt, minLength || 1, customAlphabet);
}


encode(key: number): string {
if (!Number.isInteger(key) || key < 0) {
throw new TypeError('Key must be a non-negative integer');
}

const encrypted = this.skip32.encrypt(key);

return this.hashids.encode(encrypted);
}


decode(key: string): number {
const encrypted = this.hashids.decode(key)[0];

if (encrypted === undefined) {
return null;
}

return this.skip32.decrypt(encrypted);
}
}


// Private methods

// Returns whether a given value represents a single byte
function isByte(val: number) {
return Number.isInteger(val) && val >= 0 && val <= 255;
};

0 comments on commit 80717f0

Please sign in to comment.