forked from pcarrier/gauth
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gauth.go
146 lines (133 loc) · 3.45 KB
/
gauth.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
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/sha1"
"crypto/sha256"
"encoding/base32"
"encoding/csv"
"fmt"
"golang.org/x/crypto/ssh/terminal"
"io/ioutil"
"log"
"math/big"
"os/user"
"path"
"strings"
"syscall"
"time"
)
func TimeStamp() (int64, int) {
time := time.Now().Unix()
return time / 30, int(time % 30)
}
func normalizeSecret(sec string) string {
noPadding := strings.ToUpper(strings.Replace(sec, " ", "", -1))
padLength := 8 - (len(noPadding) % 8)
if padLength < 8 {
return noPadding + strings.Repeat("=", padLength)
}
return noPadding
}
func AuthCode(sec string, ts int64) (string, error) {
key, err := base32.StdEncoding.DecodeString(sec)
if err != nil {
return "", err
}
enc := hmac.New(sha1.New, key)
msg := make([]byte, 8, 8)
msg[0] = (byte)(ts >> (7 * 8) & 0xff)
msg[1] = (byte)(ts >> (6 * 8) & 0xff)
msg[2] = (byte)(ts >> (5 * 8) & 0xff)
msg[3] = (byte)(ts >> (4 * 8) & 0xff)
msg[4] = (byte)(ts >> (3 * 8) & 0xff)
msg[5] = (byte)(ts >> (2 * 8) & 0xff)
msg[6] = (byte)(ts >> (1 * 8) & 0xff)
msg[7] = (byte)(ts >> (0 * 8) & 0xff)
if _, err := enc.Write(msg); err != nil {
return "", err
}
hash := enc.Sum(nil)
offset := hash[19] & 0x0f
trunc := hash[offset : offset+4]
trunc[0] &= 0x7F
res := new(big.Int).Mod(new(big.Int).SetBytes(trunc), big.NewInt(1000000))
return fmt.Sprintf("%06d", res), nil
}
func authCodeOrDie(sec string, ts int64) string {
str, e := AuthCode(sec, ts)
if e != nil {
log.Fatal(e)
}
return str
}
func main() {
user, e := user.Current()
if e != nil {
log.Fatal(e)
}
cfgPath := path.Join(user.HomeDir, ".config/gauth.csv")
cfgContent, e := ioutil.ReadFile(cfgPath)
if e != nil {
log.Fatal(e)
}
// Support for 'openssl enc -aes-128-cbc -md sha256 -pass pass:'
if bytes.Compare(cfgContent[:8], []byte{0x53, 0x61, 0x6c, 0x74, 0x65, 0x64, 0x5f, 0x5f}) == 0 {
fmt.Printf("Encryption password: ")
passwd, e := terminal.ReadPassword(syscall.Stdin)
fmt.Printf("\n")
if e != nil {
log.Fatal(e)
}
salt := cfgContent[8:16]
rest := cfgContent[16:]
salting := sha256.New()
salting.Write([]byte(passwd))
salting.Write(salt)
sum := salting.Sum(nil)
key := sum[:16]
iv := sum[16:]
block, e := aes.NewCipher(key)
if e != nil {
log.Fatal(e)
}
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(rest, rest)
// Remove padding
i := len(rest) - 1
for rest[i] < 16 {
i--
}
cfgContent = rest[:i]
}
cfgReader := csv.NewReader(bytes.NewReader(cfgContent))
// Unix-style tabular
cfgReader.Comma = ':'
cfg, e := cfgReader.ReadAll()
if e != nil {
log.Fatal(e)
}
currentTS, progress := TimeStamp()
//prevTS := currentTS - 1
nextTS := currentTS + 1
var l int = 0
fmt.Println(" curr next")
for _, record := range cfg {
l++
name := record[0]
secret := normalizeSecret(record[1])
//prevToken := authCodeOrDie(secret, prevTS)
currentToken := authCodeOrDie(secret, currentTS)
nextToken := authCodeOrDie(secret, nextTS)
//fmt.Printf("%-30s %s \033[31m\033[1m%s \033[0m\033[32m%s \033[0m\n", name, prevToken, currentToken, nextToken)
fmt.Printf("%-36s \033[31m\033[1m%s \033[0m\033[32m%s \033[0m\n", name, currentToken, nextToken)
if l%6 == 0 {
fmt.Printf("\n")
}
// bash test: echo -e "PREV \e[31m \e[1m CURR \e[0m \e[32m NEXT \e[0m"
// bash test: echo -e "PREV \033[31m \033[1m CURR \033[0m \033[32m NEXT\033[0m"
}
fmt.Printf("[%-29s]\n", strings.Repeat("=", progress))
}