-
Notifications
You must be signed in to change notification settings - Fork 0
/
otp.go
43 lines (31 loc) · 773 Bytes
/
otp.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
package otp
import (
"errors"
"math/rand"
)
func Encrypt(plain, key []byte) ([]byte, error) {
if len(key) != len(plain) {
return []byte{}, errors.New("The plaintext and the key should have the same size.")
}
cipher := []byte{}
for index, value := range plain {
cipher = append(cipher, value ^ key[index])
}
return cipher, nil
}
func Decrypt(cipher, key []byte) ([]byte, error) {
if len(key) != len(cipher) {
return []byte{}, errors.New("The plaintext and the key should have the same size.")
}
plain := []byte{}
for index, value := range cipher {
plain = append(plain, value ^ key[index])
}
return plain, nil
}
func GenerateKey(plaintext []byte) []byte {
length := len(plaintext)
key := make([]byte, length)
rand.Read(key)
return key
}