<commit_before>var CryptoJS = require('crypto-js')
var crypto = require('crypto')

var includeFolder = require('include-folder')
var path = require('path')
var wordlists = includeFolder(path.join(__dirname, 'wordlists'))

function BIP39(language) {
  language = language || 'en'
  this.wordlist = JSON.parse(wordlists[language])
}

BIP39.prototype.mnemonicToSeed = function(mnemonic, password) {
  var options = {iterations: 2048, hasher: CryptoJS.algo.SHA512, keySize: 512/32}
  return CryptoJS.PBKDF2(mnemonic, salt(password), options).toString(CryptoJS.enc.Hex)
}

BIP39.prototype.entropyToMnemonic = function(entropy) {
  var entropyBuffer = new Buffer(entropy, 'hex')
  var hash = crypto.createHash('sha256').update(entropyBuffer).digest()

  var combined = Buffer.concat([entropyBuffer, hash])
  var bitLength = entropyBuffer.length * 8 + entropyBuffer.length / 4
  var bits = bytesToBinary([].slice.call(combined)).substr(0, bitLength)

  var chunks = (bits).match(/(.{1,11})/g)
  return chunks.map(function(binary) {
    var index = parseInt(binary, 2)
    return this.wordlist[index]
  }, this).join(' ')
}

BIP39.prototype.generateMnemonic = function(strength) {
  strength = strength || 128
  var entropy = crypto.randomBytes(strength/8).toString('hex')
  return this.entropyToMnemonic(entropy)
}

BIP39.prototype.validate = function(mnemonic) {
  mnemonic = mnemonic.split(' ')

  if (mnemonic.length % 3 !== 0) return false

  var wordlist = this.wordlist

  var belongToList = mnemonic.every(function(word) {
    return wordlist.indexOf(word) > -1
  })

  if (!belongToList) return false

  var bits = mnemonic.map(function(m) {
    var id = wordlist.indexOf(m)
    return lpad(id.toString(2), '0', 11)
  }).join('')

  var length = bits.length
  var dividerIndex = Math.floor(length / 33) * 32
  var checksum = bits.substring(dividerIndex)

  var data = bits.substring(0, dividerIndex)
  var bytes = data.match(/(.{1,8})/g).map(function(bin) {
    return parseInt(bin, 2)
  })
  var hash = crypto.createHash('sha256').update(new Buffer(bytes)).digest()
  var checksumBits = bytesToBinary([].slice.call(hash))
  var checksum2 = checksumBits.substr(0, length - dividerIndex)

  return checksum === checksum2
}

function salt(password) {
  return encode_utf8('mnemonic' + (password || ''))<commit_msg>index: avoid overwrite, use words variable<commit_after>var CryptoJS = require('crypto-js')
var crypto = require('crypto')

var includeFolder = require('include-folder')
var path = require('path')
var wordlists = includeFolder(path.join(__dirname, 'wordlists'))

function BIP39(language) {
  language = language || 'en'
  this.wordlist = JSON.parse(wordlists[language])
}

BIP39.prototype.mnemonicToSeed = function(mnemonic, password) {
  var options = {iterations: 2048, hasher: CryptoJS.algo.SHA512, keySize: 512/32}
  return CryptoJS.PBKDF2(mnemonic, salt(password), options).toString(CryptoJS.enc.Hex)
}

BIP39.prototype.entropyToMnemonic = function(entropy) {
  var entropyBuffer = new Buffer(entropy, 'hex')
  var hash = crypto.createHash('sha256').update(entropyBuffer).digest()

  var combined = Buffer.concat([entropyBuffer, hash])
  var bitLength = entropyBuffer.length * 8 + entropyBuffer.length / 4
  var bits = bytesToBinary([].slice.call(combined)).substr(0, bitLength)

  var chunks = (bits).match(/(.{1,11})/g)
  return chunks.map(function(binary) {
    var index = parseInt(binary, 2)
    return this.wordlist[index]
  }, this).join(' ')
}

BIP39.prototype.generateMnemonic = function(strength) {
  strength = strength || 128
  var entropy = crypto.randomBytes(strength/8).toString('hex')
  return this.entropyToMnemonic(entropy)
}

BIP39.prototype.validate = function(mnemonic) {
  var words = mnemonic.split(' ')

  if (words.length % 3 !== 0) return false

  var wordlist = this.wordlist
  var belongToList = words.every(function(word) {
    return wordlist.indexOf(word) > -1
  })

  if (!belongToList) return false

  var bits = words.map(function(word) {
    var id = wordlist.indexOf(word)
    return lpad(id.toString(2), '0', 11)
  }).join('')

  var length = bits.length
  var dividerIndex = Math.floor(length / 33) * 32
  var checksum = bits.substring(dividerIndex)

  var data = bits.substring(0, dividerIndex)
  var bytes = data.match(/(.{1,8})/g).map(function(bin) {
    return parseInt(bin, 2)
  })
  var hash = crypto.createHash('sha256').update(new Buffer(bytes)).digest()
  var checksumBits = bytesToBinary([].slice.call(hash))
  var checksum2 = checksumBits.substr(0, length - dividerIndex)

  return checksum === checksum2
}

function salt(password) {
  return encode_utf8('mnemonic' + (password || ''))
{'fraction_non_alphanumeric': 0.14574376612209802, 'fraction_numerical': 0.02536543422184007, 'mean_word_length': 4.716216216216216, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5404005', 'n_tokens_mistral': 1652, 'n_tokens_neox': 1548, 'n_words': 324}