// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package rc4 implements RC4 encryption, as defined in Bruce Schneier's // Applied Cryptography. // // RC4 is cryptographically broken and should not be used for secure // applications.
package rc4 import ( ) // A Cipher is an instance of RC4 using a particular key. type Cipher struct { s [256]uint32 i, j uint8 } type KeySizeError int func ( KeySizeError) () string { return "crypto/rc4: invalid key size " + strconv.Itoa(int()) } // NewCipher creates and returns a new [Cipher]. The key argument should be the // RC4 key, at least 1 byte and at most 256 bytes. func ( []byte) (*Cipher, error) { if fips140only.Enabled { return nil, errors.New("crypto/rc4: use of RC4 is not allowed in FIPS 140-only mode") } := len() if < 1 || > 256 { return nil, KeySizeError() } var Cipher for := 0; < 256; ++ { .s[] = uint32() } var uint8 = 0 for := 0; < 256; ++ { += uint8(.s[]) + [%] .s[], .s[] = .s[], .s[] } return &, nil } // Reset zeros the key data and makes the [Cipher] unusable. // // Deprecated: Reset can't guarantee that the key will be entirely removed from // the process's memory. func ( *Cipher) () { for := range .s { .s[] = 0 } .i, .j = 0, 0 } // XORKeyStream sets dst to the result of XORing src with the key stream. // Dst and src must overlap entirely or not at all. func ( *Cipher) (, []byte) { if len() == 0 { return } if alias.InexactOverlap([:len()], ) { panic("crypto/rc4: invalid buffer overlap") } , := .i, .j _ = [len()-1] = [:len()] // eliminate bounds check from loop for , := range { += 1 := .s[] += uint8() := .s[] .s[], .s[] = , [] = ^ uint8(.s[uint8(+)]) } .i, .j = , }