Commit ab7a51f4 authored by Aral Balkan's avatar Aral Balkan
Browse files

Added an optional secret to the code generation and regenerated the first batch of invite codes.

parent 21749620
node_modules
.DS_Store
\ No newline at end of file
.DS_Store
secret.txt
\ No newline at end of file
shorthash = require 'shorthash'
uuid = require 'uuid'
fs = require 'fs'
assert = require 'assert'
#
# Implementation of hash sieve algorithm that I read
# about at http://www.shamasis.net/2009/09/fast-algorithm-to-find-unique-items-in-javascript-array/
# in CoffeeScript with slightly more literate code :)
#
Array.prototype.unique = ->
hashSieve = {}
arrayOfUniqueValues = [];
for item in @
hashSieve[item] = item
for item of hashSieve
arrayOfUniqueValues.push item
return arrayOfUniqueValues
class CodeGenerator
secret: ''
folder: null
folderExists: false
......@@ -12,18 +32,28 @@ class CodeGenerator
# do not accidentally overwrite earlier ones
@folder = 'codes/' + Date.now()
if fs.existsSync('secret.txt')
@secret = fs.readFileSync 'secret.txt'
generateUniqueCodes: (numCodes) =>
codes = []
i = numCodes
while numCodes
uniqueCode = uuid.v4() + '-' + Date.now()
while i
uniqueCode = uuid.v4() + @secret + '-' + Date.now()
shortUniqueCode = shorthash.unique(uniqueCode)
codes.push shortUniqueCode
numCodes--
i--
codes = codes.unique()
# Just to make sure.
assert.equal codes.length, numCodes, 'All codes must be unique.'
return codes
generateUniqueCodesAndSaveToFile: (numCodes, fileNameToSaveTo) =>
codes = @generateUniqueCodes(numCodes)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment