-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_otp.go
153 lines (132 loc) · 3.33 KB
/
ft_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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/rand"
"crypto/sha1"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"hash"
"os"
"strings"
"time"
)
const periodicity = 30
const numDig = 6
const saveKey = 1
const genPass = 2
const tenPowSix = 1000000
var b = []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
func generatePass(decryptedKey []byte) {
timeInt64 := time.Now().Unix() / periodicity
timeBytes := make([]byte, 8)
binary.BigEndian.PutUint64(timeBytes, uint64(timeInt64))
var hasher hash.Hash = hmac.New(sha1.New, decryptedKey)
hasher.Write(timeBytes)
var hashedBytesTimeKey []byte = hasher.Sum(nil)
lastFourBits := hashedBytesTimeKey[19] & 0xf
binCode := (int(hashedBytesTimeKey[lastFourBits]&0x7f)<<24 | int(hashedBytesTimeKey[lastFourBits+1])<<16 |
int(hashedBytesTimeKey[lastFourBits+2])<<8 | int(hashedBytesTimeKey[lastFourBits+3])) % tenPowSix
fmt.Printf("%06d", binCode)
}
func decryptKey(fileName string) ([]byte, error) {
encryptedKey, errEncKey := os.ReadFile(fileName)
myKey, errMyKey := os.ReadFile("myKey.key")
if errEncKey != nil || errMyKey != nil {
return nil, errEncKey
}
block, err := aes.NewCipher(myKey)
if err != nil {
return nil, err
}
encrypter := cipher.NewCFBDecrypter(block, b)
decryptedKey := make([]byte, len(encryptedKey))
encrypter.XORKeyStream(decryptedKey, encryptedKey)
return decryptedKey, nil
}
func parseHex(undecKey []byte) ([]byte, error) {
decodedKey := make([]byte, hex.DecodedLen(len(undecKey)))
numBytes, err := hex.Decode(decodedKey, undecKey)
if err != nil || numBytes == 0 {
return nil, err
}
return decodedKey, nil
}
func encryptKey(fileName string) error {
key, err := os.ReadFile(fileName)
if err != nil {
return err
}
if len(key) < 64 {
return errors.New("error: key must be 64 hexadecimal characters.")
}
decodedKey, err := parseHex(key)
if err != nil {
return err
}
myKey := make([]byte, 32)
_, err = rand.Read(myKey)
if err != nil {
return err
}
block, err := aes.NewCipher(myKey)
if err != nil {
return err
}
encrypter := cipher.NewCFBEncrypter(block, b)
encryptedKey := make([]byte, len(decodedKey))
encrypter.XORKeyStream(encryptedKey, decodedKey)
otpKeyWriter, err := os.Create("ft_otp.key")
if err != nil {
return err
}
if _, err = otpKeyWriter.Write(encryptedKey); err != nil {
return err
}
defer otpKeyWriter.Close()
myKeyWriter, err := os.Create("myKey.key")
if err != nil {
return err
}
if _, err = myKeyWriter.Write(myKey); err != nil {
return err
}
defer myKeyWriter.Close()
fmt.Println("Key was successfully saved in ft_otp.key.")
return nil
}
func readArgs() (string, int, error) {
args := os.Args
if len(args) != 3 {
return "", 0, errors.New("Wrong number of args")
}
fileName := args[2]
if args[1] == "-g" && strings.HasSuffix(fileName, ".hex") {
return fileName, saveKey, nil
} else if args[1] == "-k" && strings.HasSuffix(fileName, ".key") {
return fileName, genPass, nil
} else {
return fileName, 0, errors.New("Wrong args")
}
}
func main() {
fileName, act, err := readArgs()
if err != nil {
fmt.Println(err)
}
if act == saveKey {
if err := encryptKey(fileName); err != nil {
fmt.Println(err)
}
} else if act == genPass {
decryptedKey, err := decryptKey(fileName)
if err != nil {
fmt.Println(err)
return
}
generatePass(decryptedKey)
}
}