/* Based on hashcash */
const crypto = require("crypto")
const alphabet = `0123456789_;:. ?!@#$abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`
const next = array => {
for (i = array.length - 1; i >= 0; i--) {
if (array[i] < alphabet.length - 1) {
array[i] += 1
return true
} else {
array[i] = 0
}
}
return false
}
const toSuffix = array => {
return array.map(v => {
return alphabet[v]
}).join('')
}
const hash = data => {
const alg = crypto.createHash('sha1')
alg.update(data)
return alg.digest('hex')
}
const findHash = obj => {
const data = JSON.stringify(obj)
const zeroes = Math.min(Math.ceil(data.length / 28), 10)
console.log(`Looking for ${zeroes} zeroes...`)
for (let l = 0; l < 25; l++) {
let array = Array(l)
for (let i = 0; i < l; i++) array[i] = 0
do {
const appended = toSuffix(array)
const challenge = data + appended
const cash = hash(challenge)
if (cash.match(`^0{${zeroes}}`)) {
return {
hash: cash,
appended
}
}
} while (next(array))
}
}
const hashFound = findHash({
some: 'data', more: 'things',
andOther: 'stuff', for: 'XRP ledger',
because: ['Safe', 'Secure', 'Cool']
})
console.log(hashFound)