
const fill2Len = (str, len) => {
  if (!str) {
    return '*'.repeat(len)
  }

  const orginLen = str.length
  if (len <= orginLen) {
    return str.substr(0, len)
  }

  return str.repeat(Math.floor(len / orginLen)) + str.substr(0, len % orginLen)
}

const mixChars = window.navigator.userAgent //+ (new Date).toUTCString().replace(/\d{2}:\d{2}:\d{2}/, '')

const strXOR = (str, mix) => {
  if (!str) return str

  const strLen = str.length
  const mixStr = fill2Len(mix, strLen)

  const result = []
  for (let i = 0; i < strLen; i ++) {
    result.push(String.fromCharCode(str.charCodeAt(i) ^ mixStr.charCodeAt(i)))
  }

  return result.join('')
}

export default str => strXOR(str, mixChars)
