Skip to content

Instantly share code, notes, and snippets.

@chunrapeepat
Created August 7, 2021 12:38
Show Gist options
  • Save chunrapeepat/a7a3d385444fa57cc9891c6bbf48358b to your computer and use it in GitHub Desktop.
Save chunrapeepat/a7a3d385444fa57cc9891c6bbf48358b to your computer and use it in GitHub Desktop.
Seed phrase + secret for preventing hack, PoC
const phrase = "A B C D E F G H I J K L"
const secret = "helloworld123"
console.log(`Phase = ${phrase}`)
console.log(`Secret = ${secret}`)
console.log(`Phase + Secret = ${encode(phrase, secret)} (<-- note this on paper)`)
if (phrase !== decode(encode(phrase, secret), secret)) {
console.log("ERROR")
}
// ------------------------
function encode(phrase, secret) {
const swapList = _generateSwapList(secret)
const seeds = phrase.split(' ')
for (let i = 0; i < swapList.length; i += 2) {
const temp = seeds[swapList[i]]
seeds[swapList[i]] = seeds[swapList[i+1]]
seeds[swapList[i+1]] = temp
}
return seeds.join(' ')
}
function decode(encodedPhrase, secret) {
const swapList = _generateSwapList(secret)
const seeds = encodedPhrase.split(' ')
for (let i = swapList.length - 1; i >= 0; i -= 2) {
const temp = seeds[swapList[i]]
seeds[swapList[i]] = seeds[swapList[i-1]]
seeds[swapList[i-1]] = temp
}
return seeds.join(' ')
}
function _generateSwapList(secret) {
const list = secret.split('').map(x => x.charCodeAt() % 12)
if (list.length % 2 === 1) {
list.push(list[list.length - 1])
}
return list
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment