Created
January 4, 2016 20:48
-
-
Save SplittyDev/aaa16d4f2fb3cc003652 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var crypto = require('crypto') | |
var alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' | |
function nextRand (lower, upper) { | |
'use strict' | |
return Math.floor(Math.random() * (upper - lower + 1) + lower) | |
} | |
function generateId () { | |
'use strict' | |
var rounds = [ | |
alphabet[nextRand(0, 31)], | |
alphabet[nextRand(0, 31)], | |
alphabet[nextRand(0, 31)], | |
alphabet[nextRand(0, 31)], | |
alphabet[nextRand(0, 31)] | |
] | |
return rounds.join('') | |
} | |
function generateKey (id) { | |
'use strict' | |
var hash = crypto.createHash('md5') | |
hash = hash.update(id) | |
hash = hash.digest('hex') | |
hash = hash.replace('-', '') | |
hash = hash.toLowerCase() | |
var i, num, next, key = '' | |
for (i = 0; i < 32; i += 2) { | |
num = hash[i].concat(hash[i + 1]) | |
next = parseInt(num, 16) & 31 | |
if (i % 8 === 0 && i > 0) { | |
key += '-' | |
} | |
key += alphabet[next] | |
} | |
return key | |
} | |
function keygen () { | |
'use strict' | |
var id = generateId() | |
var key = generateKey(id) | |
console.log('ID: ' + id) | |
console.log('Key: ' + key) | |
} | |
keygen() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment