-
Notifications
You must be signed in to change notification settings - Fork 0
/
rc4.go
40 lines (36 loc) · 948 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
package pwd
import (
"crypto/rc4"
"fmt"
"strings"
"github.com/palp1tate/go-crypto-guard"
)
func GenRC4(password, rc4Key string) (encryptedPassword string, err error) {
cipher, err := rc4.NewCipher([]byte(rc4Key))
if err != nil {
return
}
ciphertext := make([]byte, len(password))
cipher.XORKeyStream(ciphertext, []byte(password))
encryptedPassword = fmt.Sprintf("%s$%s", pwd.RC4, pwd.Encode2string(ciphertext))
return
}
func VerifyRC4(password, encryptedPassword, rc4Key string) (isValid bool, err error) {
parts := strings.Split(encryptedPassword, "$")
if len(parts) != 2 {
err = fmt.Errorf("invalid encrypted password format")
return
}
cipherTextBytes, err := pwd.Decode2byte(parts[1])
if err != nil {
return
}
cipher, err := rc4.NewCipher([]byte(rc4Key))
if err != nil {
return
}
dst := make([]byte, len(cipherTextBytes))
cipher.XORKeyStream(dst, cipherTextBytes)
isValid = password == string(dst)
return
}