Comparing version 1.0.2 to 1.1.0
Changelog | ||
------- | ||
# CHANGELOG | ||
**1.0.2** | ||
**1.1.0**: | ||
- Removed `preferGlobal` from `package.json` (thanks to [@waynebloss](https://github.com/ivanakimov/hashids.node.js/issues/17)) | ||
- Moved out changelog to `CHANGELOG.md` | ||
- ES6 source; ES5 dist | ||
- UMD support ([Node.js repo](https://github.com/ivanakimov/hashids.node.js) is merging into this one) | ||
- Added Eslint (npm run lint) | ||
- Added Mocha (npm run test) | ||
- Added Coveralls (npm run coverage) | ||
- Added build script (npm run build) | ||
- Moved CHANGELOG out of `README.md` | ||
- `README.md` completely updated | ||
- `examples/` folder removed; all examples are now in the README | ||
- [Bug fix](https://github.com/ivanakimov/hashids.node.js/issues/26): escaping regex | ||
- Improvement: relaxed parameter checks to `encode()`. All of these are allowed: | ||
**1.0.1** | ||
```javascript | ||
var hashids = new Hashids(); | ||
- Auto-initialize a new instance of Hashids in case it wasn't initialized with "new" (thanks to [@rfink](https://github.com/ivanakimov/hashids.node.js/pull/15)) | ||
hashids.encode(1, 2, 3); // o2fXhV | ||
hashids.encode([1, 2, 3]); // o2fXhV | ||
hashids.encode('1', '2', '3'); // o2fXhV | ||
hashids.encode(['1', '2', '3']); // o2fXhV | ||
``` | ||
**1.0.0** | ||
**1.0.2**: | ||
- Support for older browsers (using `charAt`) by [@tauanz](https://github.com/tauanz): <https://github.com/ivanakimov/hashids.js/pull/15> | ||
**1.0.1**: | ||
- *require.js* support by [@nleclerc](https://github.com/nleclerc): <https://github.com/ivanakimov/hashids.js/pull/12> | ||
**1.0.0**: | ||
- Several public functions are renamed to be more appropriate: | ||
@@ -21,3 +42,3 @@ - Function `encrypt()` changed to `encode()` | ||
- Function `decryptHex()` changed to `decodeHex()` | ||
Hashids was designed to encode integers, primary ids at most. We've had several requests to encrypt sensitive data with Hashids and this is the wrong algorithm for that. So to encourage more appropriate use, `encrypt/decrypt` is being "downgraded" to `encode/decode`. | ||
@@ -28,21 +49,7 @@ | ||
**0.3.3** | ||
**0.3.0**: | ||
- `.toString()` added in `encryptHex()`: [https://github.com/ivanakimov/hashids.node.js/pull/9](https://github.com/ivanakimov/hashids.node.js/pull/9) (thanks to [@namuol](https://github.com/namuol)) | ||
**0.3.2** | ||
- minor: contact email changed | ||
- minor: internal version is accurate now | ||
**0.3.1** | ||
- minor: closure + readme update merged (thanks to [@krunkosaurus](https://github.com/krunkosaurus)) | ||
- minor: a few cleanups | ||
**0.3.0** | ||
**PRODUCED HASHES IN THIS VERSION ARE DIFFERENT THAN IN 0.1.4, DO NOT UPDATE IF YOU NEED THEM TO KEEP WORKING:** | ||
- Same algorithm as [PHP version](https://github.com/ivanakimov/hashids.php) now | ||
- Same algorithm as [PHP](https://github.com/ivanakimov/hashids.php) and [Node.js](https://github.com/ivanakimov/hashids.node.js) versions now | ||
- Overall approximately **4x** faster | ||
@@ -53,8 +60,7 @@ - Consistent shuffle function uses slightly modified version of [Fisher–Yates algorithm](http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm) | ||
- Basic English curse words will now be avoided even with custom alphabet | ||
- New unit tests with [Jasmine](https://github.com/mhevery/jasmine-node) | ||
- Support for MongoDB ObjectId | ||
- _encrypt_ function now also accepts array of integers as input | ||
- Passing JSLint now | ||
- Support for [Bower](http://bower.io/) package manager | ||
**0.1.4** | ||
**0.1.4**: | ||
@@ -64,3 +70,3 @@ - Global var leak for hashSplit (thanks to [@BryanDonovan](https://github.com/BryanDonovan)) | ||
**0.1.3** | ||
**0.1.3**: | ||
@@ -72,3 +78,3 @@ Warning: If you are using 0.1.2 or below, updating to this version will change your hashes. | ||
**0.1.2** | ||
**0.1.2**: | ||
@@ -86,3 +92,3 @@ Warning: If you are using 0.1.1 or below, updating to this version will change your hashes. | ||
**0.1.1** | ||
**0.1.1**: | ||
@@ -92,4 +98,4 @@ - Speed improvements | ||
**0.1.0** | ||
**0.1.0**: | ||
- First commit |
{ | ||
"author": "Ivan Akimov <ivan@barreleye.com> (https://twitter.com/IvanAkimov)", | ||
"name": "hashids", | ||
"description": "A small Node.js class to generate YouTube-like hashids from one or many numbers. Use hashids when you do not want to expose your database ids to the user. ", | ||
"version": "1.0.2", | ||
"homepage": "http://hashids.org/node-js", | ||
"description": "Generate YouTube-like ids from numbers. Use Hashids when you do not want to expose your database ids to the user.", | ||
"version": "1.1.0", | ||
"homepage": "http://hashids.org/javascript", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/ivanakimov/hashids.node.js" | ||
"url": "https://github.com/ivanakimov/hashids.js" | ||
}, | ||
"main": "lib/hashids.js", | ||
"bugs": { | ||
"url": "https://github.com/ivanakimov/issues" | ||
}, | ||
"main": "dist/hashids.js", | ||
"scripts": { | ||
"lint": "eslint hashids.js tests", | ||
"test": "mocha tests --compilers js:babel-core/register", | ||
"coverage": "nyc npm test && nyc report --reporter=text-lcov | coveralls", | ||
"build:node": "babel hashids.js -o dist/hashids.js", | ||
"rename:global": "sed -i '' 's/global.hashids/global.Hashids/g' dist/hashids.js", | ||
"minify": "uglifyjs dist/hashids.js -o dist/hashids.min.js --source-map dist/hashids.min.map --compress --mangle", | ||
"build": "npm run test && npm run build:node && npm run rename:global && npm run minify", | ||
"clean": "rm -rf coverage .nyc_output npm-debug.log", | ||
"all": "npm run lint && npm run coverage && npm run build && npm run clean" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": {}, | ||
"optionalDependencies": {}, | ||
"devDependencies": { | ||
"babel-cli": "^6.10.1", | ||
"babel-core": "^6.10.4", | ||
"babel-plugin-add-module-exports": "^0.2.1", | ||
"babel-plugin-transform-es2015-modules-umd": "^6.8.0", | ||
"babel-preset-es2015": "^6.9.0", | ||
"chai": "^3.5.0", | ||
"coveralls": "^2.11.9", | ||
"eslint": "^3.0.1", | ||
"mocha": "^2.5.3", | ||
"nyc": "^7.0.0", | ||
"uglify-js": "^2.7.0" | ||
}, | ||
"license": "MIT", | ||
"keywords": ["hashids", "hashid", "hash", "ids", "youtube", "bitly", "obfuscate", "encrypt", "decrypt", "encode", "decode"], | ||
"engines": { | ||
"node": "*" | ||
} | ||
"keywords": [ | ||
"hashids", | ||
"hashid", | ||
"hash", | ||
"ids", | ||
"youtube", | ||
"bitly", | ||
"obfuscate", | ||
"encode", | ||
"decode", | ||
"encrypt", | ||
"decrypt" | ||
] | ||
} |
292
README.md
Hashids | ||
------- | ||
[![hashids](http://hashids.org/public/img/hashids.gif "Hashids")](http://hashids.org/) | ||
A small Node.js class to generate YouTube-like ids from one or many numbers. Use hashids when you do not want to expose your database ids to the user. Read **full documentation** at: [http://hashids.org/node-js](http://hashids.org/node-js) | ||
[![Build Status][travis-image]][travis-url] | ||
[![Coveralls Status][coveralls-image]][coveralls-url] | ||
[![NPM downloads][npm-downloads-image]][npm-url] | ||
[![NPM version][npm-version-image]][npm-url] | ||
[![License][license-image]][license-url] | ||
[![Chat][chat-image]][chat-url] | ||
[![hashids](https://api.travis-ci.org/ivanakimov/hashids.node.js.svg "Hashids")](https://travis-ci.org/ivanakimov/hashids.node.js) | ||
**Hashids** is small JavaScript library to generate YouTube-like ids from numbers. Use it when you don't want to expose your database ids to the user: [http://hashids.org/javascript](http://hashids.org/javascript) | ||
Installation | ||
Getting started | ||
------- | ||
1. Node it up: [http://nodejs.org/download/](http://nodejs.org/download/) | ||
2. Install using npm: | ||
`npm install hashids` | ||
Updating from v0.3 to 1.0? | ||
------- | ||
Install Hashids via: | ||
Read the `CHANGELOG` at the bottom of this readme! | ||
- [node.js](): `npm install --save hashids` | ||
- [bower](http://bower.io/): `bower install hashids` | ||
- [jam](http://jamjs.org/): `jam install hashids` | ||
Client-side Version | ||
------- | ||
(or just use the code at `dist/hashids.js`) | ||
If you're looking for a client-side Bower version, there's a separate repo: <https://github.com/ivanakimov/hashids.js/> | ||
Use in the browser (wherever ES5 is supported; 5KB): | ||
Production Note | ||
------- | ||
```javascript | ||
<script type="text/javascript" src="hashids.min.js"></script> | ||
<script type="text/javascript"> | ||
**BE CAREFUL WHICH VERSION OF HASHIDS YOU ARE USING.** | ||
var hashids = new Hashids(); | ||
console.log(hashids.encode(1)); | ||
Since future improvements to Hashids might alter produced hashes, it's a good idea to specify *exact* Hashids version in your **package.json**, if their consistency is important to you (if you are storing them in database): | ||
</script> | ||
``` | ||
Use in Node.js: | ||
```javascript | ||
"dependencies": { | ||
"hashids": "1.0.1" | ||
} | ||
var Hashids = require('hashids'); | ||
var hashids = new Hashids(); | ||
console.log(hashids.encode(1)); | ||
``` | ||
Usage | ||
Quick example | ||
------- | ||
#### Encoding one number | ||
You can pass a unique salt value so your ids differ from everyone else's. I use "this is my salt" as an example. | ||
```javascript | ||
var hashids = new Hashids(); | ||
var Hashids = require("hashids"), | ||
hashids = new Hashids("this is my salt"); | ||
var id = hashids.encode(12345); | ||
var id = hashids.encode(1, 2, 3); // o2fXhV | ||
var numbers = hashids.decode(id); // [1, 2, 3] | ||
``` | ||
`id` is now going to be: | ||
NkK9 | ||
More options | ||
------- | ||
#### Decoding | ||
**A few more ways to pass to `encode()`:** | ||
Notice during decoding, same salt value is used: | ||
```javascript | ||
var hashids = new Hashids(); | ||
var Hashids = require("hashids"), | ||
hashids = new Hashids("this is my salt"); | ||
var numbers = hashids.decode("NkK9"); | ||
console.log(hashids.encode(1, 2, 3)); // o2fXhV | ||
console.log(hashids.encode([1, 2, 3])); // o2fXhV | ||
console.log(hashids.encode('1', '2', '3')); // o2fXhV | ||
console.log(hashids.encode(['1', '2', '3'])); // o2fXhV | ||
``` | ||
`numbers` is now going to be: | ||
[ 12345 ] | ||
**Make your ids unique:** | ||
#### Decoding with different salt | ||
Pass a project name to make your ids unique: | ||
Decoding will not work if salt is changed: | ||
```javascript | ||
var hashids = new Hashids('My Project'); | ||
console.log(hashids.encode(1, 2, 3)); // Z4UrtW | ||
var Hashids = require("hashids"), | ||
hashids = new Hashids("this is my pepper"); | ||
var numbers = hashids.decode("NkK9"); | ||
var hashids = new Hashids('My Other Project'); | ||
console.log(hashids.encode(1, 2, 3)); // gPUasb | ||
``` | ||
`numbers` is now going to be: | ||
[] | ||
#### Encoding several numbers | ||
**Use padding to make your ids longer:** | ||
```javascript | ||
Note that ids are only padded to fit **at least** a certain length. It doesn't mean that your ids will be *exactly* that length. | ||
var Hashids = require("hashids"), | ||
hashids = new Hashids("this is my salt"); | ||
var id = hashids.encode(683, 94108, 123, 5); | ||
``` | ||
`id` is now going to be: | ||
aBMswoO2UB3Sj | ||
You can also pass an array: | ||
```javascript | ||
var hashids = new Hashids(); // no padding | ||
console.log(hashids.encode(1)); // jR | ||
var arr = [683, 94108, 123, 5]; | ||
var id = hashids.encode(arr); | ||
var hashids = new Hashids('', 10); // pad to length 10 | ||
console.log(hashids.encode(1)); // VolejRejNm | ||
``` | ||
#### Decoding is done the same way | ||
**Pass a custom alphabet:** | ||
```javascript | ||
var Hashids = require("hashids"), | ||
hashids = new Hashids("this is my salt"); | ||
var numbers = hashids.decode("aBMswoO2UB3Sj"); | ||
var hashids = new Hashids('', 0, 'abcdefghijklmnopqrstuvwxyz'); // all lowercase | ||
console.log(hashids.encode(1, 2, 3)); // mdfphx | ||
``` | ||
`numbers` is now going to be: | ||
[ 683, 94108, 123, 5 ] | ||
#### Encoding and specifying minimum id length | ||
**Encode hex instead of numbers:** | ||
Here we encode integer 1, and set the **minimum** id length to **8** (by default it's **0** -- meaning ids will be the shortest possible length). | ||
Useful if you want to encode [Mongo](https://www.mongodb.com/)'s ObjectIds. Note that *there is no limit* on how large of a hex number you can pass (it does not have to be Mongo's ObjectId). | ||
```javascript | ||
var hashids = new Hashids(); | ||
var Hashids = require("hashids"), | ||
hashids = new Hashids("this is my salt", 8); | ||
var id = hashids.encode(1); | ||
var id = hashids.encodeHex('507f1f77bcf86cd799439011'); // y42LW46J9luq3Xq9XMly | ||
var hex = hashids.decodeHex(id); // 507f1f77bcf86cd799439011 | ||
``` | ||
`id` is now going to be: | ||
gB0NV05e | ||
#### Decoding | ||
Pitfalls | ||
------- | ||
```javascript | ||
1. When decoding, output is always an array of numbers (even if you encode only one number): | ||
var Hashids = require("hashids"), | ||
hashids = new Hashids("this is my salt", 8); | ||
```javascript | ||
var hashids = new Hashids(); | ||
var numbers = hashids.decode("gB0NV05e"); | ||
``` | ||
var id = hashids.encode(1); | ||
console.log(hashids.decode(id)); // [1] | ||
``` | ||
`numbers` is now going to be: | ||
[ 1 ] | ||
#### Specifying custom id alphabet | ||
2. Encoding negative numbers is not supported. | ||
3. If you pass bogus input to `encode()`, an empty string will be returned: | ||
Here we set the alphabet to consist of valid hex characters: "0123456789abcdef" | ||
```javascript | ||
var hashids = new Hashids(); | ||
```javascript | ||
var id = hashids.encode('a123'); | ||
console.log(id === ''); // true | ||
``` | ||
var Hashids = require("hashids"), | ||
hashids = new Hashids("this is my salt", 0, "0123456789abcdef"); | ||
4. Do not use this library as a security tool and do not encode sensitive data. This is **not** an encryption library. | ||
var id = hashids.encode(1234567); | ||
``` | ||
`id` is now going to be: | ||
b332db5 | ||
MongoDB Support | ||
------- | ||
MongoDB uses hex strings for their ObjectIds. You can convert them to Hashids like this: | ||
```javascript | ||
var Hashids = require("hashids"), | ||
hashids = new Hashids("this is my salt"); | ||
var id = hashids.encodeHex("507f191e810c19729de860ea"); | ||
var objectId = hashids.decodeHex(id); | ||
``` | ||
`id` will be: | ||
yNyaoWeKWVINWqvaM9bw | ||
`objectId` will be as expected: | ||
507f191e810c19729de860ea | ||
The length of the hex string does not matter -- it does not have to be a MongoDB ObjectId. | ||
Randomness | ||
------- | ||
The primary purpose of hashids is to obfuscate ids. It's not meant or tested to be used for security purposes or compression. | ||
Having said that, this algorithm does try to make these hashes unguessable and unpredictable: | ||
The primary purpose of Hashids is to obfuscate ids. It's not meant or tested to be used as a security or compression tool. Having said that, this algorithm does try to make these ids random and unpredictable: | ||
#### Repeating numbers | ||
No repeating patterns showing there are 3 identical numbers in the id: | ||
```javascript | ||
var Hashids = require("hashids"), | ||
hashids = new Hashids("this is my salt"); | ||
var id = hashids.encode(5, 5, 5, 5); | ||
var hashids = new Hashids(); | ||
console.log(hashids.encode(5, 5, 5)); // A6t1tQ | ||
``` | ||
You don't see any repeating patterns that might show there's 4 identical numbers in the id: | ||
1Wc8cwcE | ||
Same with incremented numbers: | ||
```javascript | ||
var hashids = new Hashids(); | ||
var Hashids = require("hashids"), | ||
hashids = new Hashids("this is my salt"); | ||
console.log(hashids.encode(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); // wpfLh9iwsqt0uyCEFjHM | ||
var id = hashids.encode(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); | ||
console.log(hashids.encode(1)); // jR | ||
console.log(hashids.encode(2)); // k5 | ||
console.log(hashids.encode(3)); // l5 | ||
console.log(hashids.encode(4)); // mO | ||
console.log(hashids.encode(5)); // nR | ||
``` | ||
`id` will be : | ||
kRHnurhptKcjIDTWC3sx | ||
#### Incrementing number ids: | ||
```javascript | ||
var Hashids = require("hashids"), | ||
hashids = new Hashids("this is my salt"); | ||
var id1 = hashids.encode(1), /* NV */ | ||
id2 = hashids.encode(2), /* 6m */ | ||
id3 = hashids.encode(3), /* yD */ | ||
id4 = hashids.encode(4), /* 2l */ | ||
id5 = hashids.encode(5); /* rD */ | ||
``` | ||
Curses! #$%@ | ||
------- | ||
This code was written with the intent of placing created hashes in visible places - like the URL. Which makes it unfortunate if generated hashes accidentally formed a bad word. | ||
This code was written with the intent of placing created ids in visible places, like the URL. Therefore, the algorithm tries to avoid generating most common English curse words by generating ids that never have the following letters next to each other: | ||
Therefore, the algorithm tries to avoid generating most common English curse words. This is done by never placing the following letters next to each other: | ||
c, C, s, S, f, F, h, H, u, U, i, I, t, T | ||
c, f, h, i, s, t, u | ||
Running tests | ||
Support | ||
------- | ||
Hashids uses [jasmine](http://pivotal.github.io/jasmine/) spec tests, particularly [jasmine-node](https://npmjs.org/package/jasmine-node). | ||
To install `sudo npm install -g jasmine-node` | ||
then just run `jasmine-node .` in the root folder. | ||
Have a question? Ping me [@IvanAkimov](http://twitter.com/ivanakimov) or [ivanakimov.com](http://ivanakimov.com) | ||
Contact | ||
License | ||
------- | ||
Follow me [@IvanAkimov](http://twitter.com/ivanakimov) | ||
MIT License. See the `LICENSE` file. You can use Hashids in open source projects and commercial products. Don't break the Internet. Kthxbye. | ||
Or [http://ivanakimov.com](http://ivanakimov.com) | ||
[travis-url]: https://travis-ci.org/ivanakimov/hashids.js | ||
[travis-image]: https://travis-ci.org/ivanakimov/hashids.js.svg | ||
License | ||
------- | ||
[coveralls-url]: https://coveralls.io/github/ivanakimov/hashids.js | ||
[coveralls-image]: https://coveralls.io/repos/github/ivanakimov/hashids.js/badge.svg | ||
MIT License. See the `LICENSE` file. You can use Hashids in open source projects and commercial products. Don't break the Internet. Kthxbye. | ||
[npm-downloads-image]: https://img.shields.io/npm/dm/hashids.svg?style=flat-square | ||
[npm-version-image]: https://img.shields.io/npm/v/hashids.svg | ||
[npm-url]: https://www.npmjs.com/package/hashids | ||
[license-url]: https://github.com/ivanakimov/hashids.js/blob/master/LICENSE | ||
[license-image]: https://img.shields.io/packagist/l/hashids/hashids.svg?style=flat | ||
[chat-url]: https://gitter.im/hashids/hashids?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge | ||
[chat-image]: https://badges.gitter.im/Join%20Chat.svg |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
52923
23
826
0
11
193
1