Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial implementation

  • Loading branch information...
commit 41c9067f9a5f3408b90b9abcf64ff823426e2d1e 1 parent 4dba077
@faeldt authored
View
10 .gitignore
@@ -0,0 +1,10 @@
+.DS_Store
+lib-cov
+*.seed
+*.log
+*.csv
+*.dat
+*.out
+*.pid
+benchmarks/*.png
+node_modules
View
3  .npmignore
@@ -0,0 +1,3 @@
+support
+test
+examples
View
18 README.md
@@ -1,2 +1,18 @@
base64id
-========
+========
+
+Node.js module that generates a base64 id.
+
+Uses crypto.randomBytes when available, falls back to unsafe methods for node.js <= 0.4.
+
+To increase performance, random bytes are buffered to minimize the number of synchronous calls to crypto.randomBytes.
+
+## Installation
+
+ $ npm install mongoose
+
+## Usage
+
+ var base64id = require('base64id');
+
+ var id = base64id.generateId();
View
103 lib/base64id.js
@@ -0,0 +1,103 @@
+/*!
+ * base64id v0.1.0
+ */
+
+/**
+ * Module dependencies
+ */
+
+var crypto = require('crypto');
+
+/**
+ * Constructor
+ */
+
+var Base64Id = function() { };
+
+/**
+ * Get random bytes
+ *
+ * Uses a buffer if available, falls back to crypto.randomBytes
+ */
+
+Base64Id.prototype.getRandomBytes = function(bytes) {
+
+ var BUFFER_SIZE = 4096
+ var self = this;
+
+ bytes = bytes || 12;
+
+ if (bytes > BUFFER_SIZE) {
+ return crypto.randomBytes(bytes);
+ }
+
+ var bytesInBuffer = parseInt(BUFFER_SIZE/bytes);
+ var threshold = parseInt(bytesInBuffer*0.85);
+
+ if (!threshold) {
+ return crypto.randomBytes(bytes);
+ }
+
+ if (this.bytesBufferIndex == null) {
+ this.bytesBufferIndex = -1;
+ }
+
+ if (this.bytesBufferIndex == bytesInBuffer) {
+ this.bytesBuffer = null;
+ this.bytesBufferIndex = -1;
+ }
+
+ // No buffered bytes available or index above threshold
+ if (this.bytesBufferIndex == -1 || this.bytesBufferIndex > threshold) {
+
+ if (!this.isGeneratingBytes) {
+ this.isGeneratingBytes = true;
+ crypto.randomBytes(BUFFER_SIZE, function(err, bytes) {
+ self.bytesBuffer = bytes;
+ self.bytesBufferIndex = 0;
+ self.isGeneratingBytes = false;
+ });
+ }
+
+ // Fall back to sync call when no buffered bytes are available
+ if (this.bytesBufferIndex == -1) {
+ return crypto.randomBytes(bytes);
+ }
+ }
+
+ var result = this.bytesBuffer.slice(bytes*this.bytesBufferIndex, bytes*(this.bytesBufferIndex+1));
+ this.bytesBufferIndex++;
+
+ return result;
+}
+
+/**
+ * Generates a base64 id
+ *
+ * (Original version from socket.io <http://socket.io>)
+ */
+
+Base64Id.prototype.generateId = function () {
+ var rand = new Buffer(15); // multiple of 3 for base64
+ if (!rand.writeInt32BE) {
+ return Math.abs(Math.random() * Math.random() * Date.now() | 0).toString()
+ + Math.abs(Math.random() * Math.random() * Date.now() | 0).toString();
+ }
+ this.sequenceNumber = (this.sequenceNumber + 1) | 0;
+ rand.writeInt32BE(this.sequenceNumber, 11);
+ if (crypto.randomBytes) {
+ this.getRandomBytes(12).copy(rand);
+ } else {
+ // not secure for node 0.4
+ [0, 4, 8].forEach(function(i) {
+ rand.writeInt32BE(Math.random() * Math.pow(2, 32) | 0, i);
+ });
+ }
+ return rand.toString('base64').replace(/\//g, '_').replace(/\+/g, '-');
+};
+
+/**
+ * Export
+ */
+
+exports = module.exports = new Base64Id();
View
12 package.json
@@ -0,0 +1,12 @@
+{
+ "name": "base64id"
+ , "version": "0.1.0"
+ , "description": "Generates a base64 id"
+ , "author": "Kristian Faeldt <faeldt_kristian@cyberagent.co.jp>"
+ , "repository": {
+ "type": "git"
+ , "url": "https://github.com/faeldt/base64id.git"
+ }
+ , "main": "./lib/base64id.js"
+ , "engines": { "node": ">= 0.4.0" }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.