Skip to content

Instantly share code, notes, and snippets.

@leodutra
Last active August 6, 2018 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leodutra/b7f386510d69d912479026d088105ba4 to your computer and use it in GitHub Desktop.
Save leodutra/b7f386510d69d912479026d088105ba4 to your computer and use it in GitHub Desktop.
Function for random base 58 string generation (good for password redefinitions)
function randomBase58(digits) {
digits = digits || 0
var base58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'.split('')
var result = ''
var char
while (result.length < digits) {
char = base58[Math.random() * 57 >> 0]
if (result.indexOf(char) === -1) result += char // avoid repetitions
}
return result
}
@kalepail
Copy link

kalepail commented Aug 6, 2018

Updated a bit as IPFS will break if the pattern Qm exists in the result.

function randomBase58(digits = 0) {
  const base58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'.split('')

  let result = ''
  let char

  while (result.length < digits) {
    char = base58[Math.random() * 57 >> 0]

    if (result.indexOf(char) === -1) result += char
    if (result.indexOf('Qm') > -1) result = ''
  }

  return result
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment