Skip to content

Instantly share code, notes, and snippets.

@SplittyDev
Created January 4, 2016 20:48
Show Gist options
  • Save SplittyDev/aaa16d4f2fb3cc003652 to your computer and use it in GitHub Desktop.
Save SplittyDev/aaa16d4f2fb3cc003652 to your computer and use it in GitHub Desktop.
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