/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
var fromCodePoint = require( '@stdlib/string/from-code-point' );
var ksa = require( './ksa.js' );
var prga = require( './prga.js' );


// MAIN //

/**
* Returns an (alleged) RC4 stream cipher.
*
*
* @param {string} key - cipher key
* @returns {Function} stream cipher
*
* @example
* var encrypt = arc4( 'pwd12' );
* var out = encrypt( 'beep boop bop' );
* // returns '...'
*
* var decrypt = arc4( 'pwd12' );
* var str = decrypt( out );
* // returns 'beep boop bop'
*/
function arc4( key ) {
	var getByte;
	var state;
	if ( !isString( key ) ) {
		throw new TypeError( 'invalid input argument. Must provide a string primitive. Value: `' + key + '`.' );
	}
	// Initialize the cipher state:
	state = ksa( key );

	// Generate a pseudorandom generation function:
	getByte = prga( state );

	return cipher;

	/**
	* Encrypts a provided string.
	*
	* @param {string} str - string to encrypt
	* @returns {string} encrypted string
	*
	* @example
	* var out = cipher( 'beep boop bop' );
	* // returns '...'
	*/
	function cipher( str ) {
		var out;
		var b1;
		var b2;
		var ch;
		var i;
		if ( !isString( str ) ) {
			throw new TypeError( 'invalid input argument. Must provide a string primitive. Value: `' + str + '`.' );
		}
		out = '';
		for ( i = 0; i < str.length; i++ ) {
			// Get a Unicode code point from the provided string:
			b1 = str.charCodeAt( i );

			// Generate a pseudorandom byte:
			b2 = getByte();

			// Generate a new character code point:
			ch = (b1 ^ b2); // bitwise XOR

			// Update the cipher text:
			out += fromCodePoint( ch );
		}
		return out;
	}
}


// EXPORTS //

module.exports = arc4;

@@ -1544,24 +1544,36 @@
  string.%0A%09*%0A
+%09* @private%0A
 %09* @param %7Bs

Add private annotation