summaryrefslogtreecommitdiffstats
path: root/xor-crypt.js
blob: d20f809e6743727e0365fb1ef96658814e7a88eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*!
 * XOR Crypt v1.1.0 - http://github.com/RobLoach/xor-crypt
 * @license MIT
 *   http://opensource.org/licenses/MIT
 */

/**
 * Universal Module Definition
 *
 * @see http://github.com/umdjs/umd
 */
(function (root, factory) {
  'use strict'
  /* global define */
  if (typeof define === 'function' && define.amd) {
    define([], factory)
  } else if (typeof exports === 'object') {
    module.exports = factory()
  } else {
    root.xorCrypt = factory()
  }
}(this, function () {
  'use strict'

  /**
   * Encrypt or decrypt a string with the given XOR key.
   *
   * @name xorCrypt
   * @param {string} str - The string to encrypt.
   * @param {int} [key=6] - The XOR key to use when encrypting.
   *
   * @return The resulting XOR'ed string.
   */
  return function xorCrypt (str, key) {
    var output = ''

    if (!key) {
      key = 6
    }

    for (var i = 0; i < str.length; ++i) {
      output += String.fromCharCode(key ^ str.charCodeAt(i))
    }

    return output
  }
}))