Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function updateTable(pairs) {
- pairs.forEach(function(pair) {
- var hash = pair[0]
- var mult = pair[1]
- var color = 'gray'
- if (mult == 3) color = 'green'
- if (mult == 5) color = 'purple'
- if (mult == 50) color = 'orange'
- var tableBody = document.getElementById("tbody");
- tableBody.innerHTML += "<tr><td>" + hash + "</td><td style='background:" + color + "'>" + mult + "</td></tr>";
- })
- }
- var maxHex = 13
- var maxInt = Math.pow(2, maxHex * 4)
- var clientSeed = '000000000000000007a9a31ff7f07463d91af6b5454241d5faf282e5e0fe1b3a'
- var pocketTypes = [{
- count: 1,
- multiplier: 50
- }, {
- count: 10,
- multiplier: 5
- }, {
- count: 17,
- multiplier: 3
- }, {
- count: 26,
- multiplier: 2
- }, ]
- function previousHash(hash) {
- return CryptoJS.SHA256(hash).toString()
- };
- function hmac(key, v) {
- var hmacHasher = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, key);
- return hmacHasher.finalize(v).toString();
- }
- function toInt(hash) {
- return parseInt(hash.slice(0, maxHex), 16)
- }
- function randomInt(hash, max) {
- hash = hmac(hash, clientSeed)
- var integer = toInt(hash)
- return Math.floor(max * (integer / maxInt))
- }
- function createPockets(pocketTypes) {
- return pocketTypes.reduce(function(result, next) {
- return result.concat(Array(next.count).fill(next.multiplier))
- }, [])
- }
- function pickRandom(hash, pockets) {
- var index = randomInt(hash, pockets.length)
- return pockets[index]
- }
- function nPrevious(hash, n, result) {
- if(result == null) result = []
- if(n == 0) return result
- var previous = previousHash(hash)
- result.push(previous)
- return nPrevious(previous,--n,result)
- }
- function getParams(name, url) {
- if (!url) url = window.location.href;
- name = name.replace(/[\[\]]/g, "\\$&");
- var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
- results = regex.exec(url);
- if (!results) return null;
- if (!results[2]) return '';
- return decodeURIComponent(results[2].replace(/\+/g, " "));
- }
- function run() {
- var currentHash = getParams('hash')
- if (currentHash == null) {
- return console.log('must supply hash as url parameter')
- }
- var previousHashes = nPrevious(currentHash, 20)
- var hashes = [currentHash].concat(previousHashes)
- var pockets = createPockets(pocketTypes)
- var results = hashes.map(function(hash) {
- return [hash, pickRandom(hash, pockets)]
- })
- updateTable(results)
- }
- run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement