[Pkg-javascript-commits] [node-uid-safe] 01/03: Imported Upstream version 1.0.1
Leo Iannacone
l3on-guest at moszumanska.debian.org
Sat Jul 12 16:15:56 UTC 2014
This is an automated email from the git hooks/post-receive script.
l3on-guest pushed a commit to branch master
in repository node-uid-safe.
commit 0f9ff9371a904e6aaea25c5881795393d1895ea3
Author: Leo Iannacone <l3on at ubuntu.com>
Date: Sat Jul 12 17:14:56 2014 +0200
Imported Upstream version 1.0.1
---
LICENSE | 22 ++++++++++++++++++++++
README.md | 44 ++++++++++++++++++++++++++++++++++++++++++++
index.js | 27 +++++++++++++++++++++++++++
package.json | 24 ++++++++++++++++++++++++
test.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 163 insertions(+)
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..a7ae8ee
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,22 @@
+
+The MIT License (MIT)
+
+Copyright (c) 2014 Jonathan Ong me at jongleberry.com
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..2178c6c
--- /dev/null
+++ b/README.md
@@ -0,0 +1,44 @@
+
+# UID Safe
+
+Create cryptographically secure UIDs safe for both cookie and URL usage.
+This is in contrast to modules such as [rand-token](https://github.com/sehrope/node-rand-token)
+and [uid2](https://github.com/coreh/uid2) whose UIDs are actually skewed
+due to the use of `%` and unnecessarily truncate the UID.
+Use this if you could still use UIDs with `-` and `_` in them.
+
+## API
+
+```js
+var uid = require('uid-safe')
+```
+
+### uid(byteLength, [cb])
+
+Asynchronously create a UID with a specific byte length.
+Because `base64` encoding is used underneath, this is not the string length!
+For example, to create a UID of length 24, you want a byte length of 18!
+
+If `cb` is not defined, a promise is returned.
+However, to use promises, you must either install [bluebird](https://github.com/petkaantonov/bluebird)
+or use a version of node.js that has native promises,
+otherwise your process will crash and die.
+
+```js
+uid(18).then(function (string) {
+ // do something with the string
+})
+
+uid(18, function (err, string) {
+ if (err) throw err
+ // do something with the string
+})
+```
+
+### uid.sync(byteLength)
+
+A synchronous version of above.
+
+```js
+var string = uid.sync(18)
+```
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..9e26461
--- /dev/null
+++ b/index.js
@@ -0,0 +1,27 @@
+
+var pseudoRandomBytes = require('crypto').pseudoRandomBytes
+var escape = require('base64-url').escape
+
+var pseudoRandomBytesProm
+
+module.exports = uid
+
+function uid(length, cb) {
+ if (cb) {
+ return pseudoRandomBytes(length, function (err, buf) {
+ if (err) return cb(err)
+ cb(null, escapeBuffer(buf))
+ })
+ }
+
+ pseudoRandomBytesProm || (pseudoRandomBytesProm = require('mz/crypto').pseudoRandomBytes)
+ return pseudoRandomBytesProm(length).then(escapeBuffer)
+}
+
+uid.sync = function uid_sync(length) {
+ return escapeBuffer(pseudoRandomBytes(length))
+}
+
+function escapeBuffer(buf) {
+ return escape(buf.toString('base64'))
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..f82510d
--- /dev/null
+++ b/package.json
@@ -0,0 +1,24 @@
+{
+ "name": "uid-safe",
+ "description": "URL and cookie safe UIDs",
+ "version": "1.0.1",
+ "author": {
+ "name": "Jonathan Ong",
+ "email": "me at jongleberry.com",
+ "url": "http://jongleberry.com",
+ "twitter": "https://twitter.com/jongleberry"
+ },
+ "license": "MIT",
+ "repository": "crypto-utils/uid-safe",
+ "dependencies": {
+ "mz": "1",
+ "base64-url": "1"
+ },
+ "devDependencies": {
+ "bluebird": "2",
+ "mocha": "1"
+ },
+ "scripts": {
+ "test": "mocha --reporter spec --bail"
+ }
+}
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..3665fb9
--- /dev/null
+++ b/test.js
@@ -0,0 +1,46 @@
+
+var assert = require('assert')
+
+var uid = require('./')
+
+describe('uid-url', function () {
+ describe('uid()', function () {
+ it('should return a uid of the correct length', function () {
+ return uid(18).then(function (val) {
+ assert.equal(24, Buffer.byteLength(val))
+ })
+ })
+
+ it('should not contain +, /, or =', function () {
+ return uid(100000).then(function (val) {
+ assert(!~val.indexOf('+'))
+ assert(!~val.indexOf('/'))
+ assert(!~val.indexOf('='))
+ })
+ })
+
+ it('should support callbacks', function (done) {
+ uid(1000000, function (err, val) {
+ if (err) return done(err)
+ assert(!~val.indexOf('+'))
+ assert(!~val.indexOf('/'))
+ assert(!~val.indexOf('='))
+ done()
+ })
+ })
+ })
+
+ describe('uid.sync()', function () {
+ it('should return a uid of the correct length', function () {
+ var val = uid.sync(18)
+ assert.equal(24, Buffer.byteLength(val))
+ })
+
+ it('should not contain +, /, or =', function () {
+ var val = uid.sync(100000)
+ assert(!~val.indexOf('+'))
+ assert(!~val.indexOf('/'))
+ assert(!~val.indexOf('='))
+ })
+ })
+})
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-uid-safe.git
More information about the Pkg-javascript-commits
mailing list