The goog.crypt.Arc4 Class

goog.crypt.Arc4()

ARC4 streamcipher implementation.

.crypt(data, opt_length)

En- or decrypt (same operation for streamciphers like ARC4)

data {Array.<number>|Uint8Array}
The data to be xor-ed in place.
opt_length {number=}
The number of bytes to crypt.

.discard(length)

Discards n bytes of the keystream. These days 1536 is considered a decent amount to drop to get the key state warmed-up enough for secure usage. This is not done in the constructor to preserve efficiency for use cases that do not need this. NOTE: Discard is identical to crypt without actually xoring any data. It's unfortunate to have this code duplicated, but this was done for performance reasons. Alternatives which were attempted: 1. Create a temp array of the correct length and pass it to crypt. This works but needlessly allocates an array. But more importantly this requires choosing an array type (Array or Uint8Array) in discard, and choosing a different type than will be passed to crypt by the client code hurts the javascript engines ability to optimize crypt (7x hit in v8). 2. Make data option in crypt so discard can pass null, this has a huge perf hit for crypt.

length {number}
Number of bytes to disregard from the stream.

.setKey(key, opt_length)

Initialize the cipher for use with new key.

key {Array.<number>}
A byte array containing the key.
opt_length {number=}
Indicates # of bytes to take from the key.