-
Notifications
You must be signed in to change notification settings - Fork 69
/
rc4.go
51 lines (44 loc) · 920 Bytes
/
rc4.go
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
48
49
50
51
package dongle
import (
"crypto/rc4"
"fmt"
)
type Rc4Error struct {
}
func NewRc4Error() Rc4Error {
return Rc4Error{}
}
func (e Rc4Error) KeyError() error {
return fmt.Errorf("rc4: invalid key, the key at least 1 byte and at most 256 bytes")
}
var rc4Error = NewRc4Error()
// ByRc4 encrypts by rc4.
func (e Encrypter) ByRc4(key []byte) Encrypter {
if len(e.src) == 0 || e.Error != nil {
return e
}
cipher, err := rc4.NewCipher(key)
if err != nil {
e.Error = rc4Error.KeyError()
return e
}
dst := make([]byte, len(e.src))
cipher.XORKeyStream(dst, e.src)
e.dst = dst
return e
}
// ByRc4 decrypts by rc4.
func (d Decrypter) ByRc4(key []byte) Decrypter {
if len(d.src) == 0 || d.Error != nil {
return d
}
cipher, err := rc4.NewCipher(key)
if err != nil {
d.Error = rc4Error.KeyError()
return d
}
dst := make([]byte, len(d.src))
cipher.XORKeyStream(dst, d.src)
d.dst = dst
return d
}