-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathslidingPassword.go
165 lines (132 loc) · 2.78 KB
/
slidingPassword.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
154
155
156
157
158
159
160
161
162
163
164
165
package main // github.com/gitinsky/vnc-go-web
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/json"
"hash/crc32"
"sync"
"time"
"github.com/satori/go.uuid"
)
type EncrData struct {
IV []byte
Data []byte
}
type RawData struct {
CSum uint32
Data []byte
}
const (
passwdLen = 16
)
func randStr(strLen int) []byte {
buf := make([]byte, strLen)
_, err := rand.Read(buf)
if err != nil {
panic(err)
}
return buf
}
type SlidingPassword struct {
curPass []byte
oldPass []byte
lock sync.RWMutex
}
func NewSlidingPassword() *SlidingPassword {
return &SlidingPassword{curPass: randStr(passwdLen), oldPass: randStr(passwdLen)}
}
func (p *SlidingPassword) UpdateLoop(interval time.Duration) {
for {
time.Sleep(interval)
p.Update()
}
}
func (p *SlidingPassword) Update() {
defer p.lock.Unlock()
p.lock.Lock()
p.oldPass = p.curPass
p.curPass = randStr(passwdLen)
}
func (p *SlidingPassword) GetPasswords() ([]byte, []byte) {
defer p.lock.RUnlock()
p.lock.RLock()
return p.curPass, p.oldPass
}
func (p *SlidingPassword) Decrypt(msg []byte) *AuthToken {
curPass, oldPass := p.GetPasswords()
res := DecryptAESCFB(msg, curPass)
if res != nil {
return res
}
res = DecryptAESCFB(msg, oldPass)
if res != nil {
return res
}
return nil
}
func (p *SlidingPassword) Encrypt(authToken *AuthToken) []byte {
curPass, _ := p.GetPasswords()
return EncryptAESCFB(authToken, curPass)
}
func uuidgen() []byte {
data, err := uuid.NewV4().MarshalBinary()
if err != nil {
panic(err)
}
return data
}
func EncryptAESCFB(src *AuthToken, key []byte) []byte {
var err error
rawData := RawData{}
rawData.Data, err = json.Marshal(src)
if err != nil {
panic(err)
}
rawData.CSum = crc32.ChecksumIEEE(rawData.Data)
rawBytes, err := json.Marshal(rawData)
encrData := EncrData{
IV: randStr(len(key)),
Data: make([]byte, len(rawBytes)),
}
cipher.NewCFBEncrypter(aesCipher(key), encrData.IV).XORKeyStream(encrData.Data, rawBytes)
encrBytes, err := json.Marshal(encrData)
if err != nil {
panic(err)
}
return encrBytes
}
func DecryptAESCFB(src, key []byte) *AuthToken {
encrData := EncrData{}
err := json.Unmarshal(src, &encrData)
if err != nil {
return nil
}
if len(encrData.IV) != len(key) {
return nil
}
rawBytes := make([]byte, len(encrData.Data))
cipher.NewCFBDecrypter(aesCipher(key), encrData.IV).XORKeyStream(rawBytes, encrData.Data)
rawData := RawData{}
err = json.Unmarshal(rawBytes, &rawData)
if err != nil {
return nil
}
cSum := crc32.ChecksumIEEE(rawData.Data)
if cSum != rawData.CSum {
return nil
}
res := AuthToken{}
err = json.Unmarshal(rawData.Data, &res)
if err != nil {
return nil
}
return &res
}
func aesCipher(key []byte) cipher.Block {
cip, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
return cip
}