-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrypt.go
More file actions
61 lines (55 loc) · 1.17 KB
/
scrypt.go
File metadata and controls
61 lines (55 loc) · 1.17 KB
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
// http://my.oschina.net/Jacker/blog/32837
// Two way encipherment ported from PHP.
package scrypt
import (
"bytes"
"crypto/md5"
"fmt"
"math/rand"
"time"
)
func keyEd(txt []byte, encryptKey string) []byte {
m := md5.New()
fmt.Fprint(m, encryptKey)
t := bytes.NewBufferString("")
keyBytes := m.Sum(nil)
for i, ctr := 0, 0; i < len(txt); i++ {
if ctr == len(keyBytes) {
ctr = 0
}
t.WriteByte(txt[i] ^ keyBytes[ctr])
ctr++
}
return t.Bytes()
}
func Encrypt(txt, key string) string {
rand.Seed(time.Nanosecond.Nanoseconds())
m := md5.New()
fmt.Fprint(m, rand.Int63())
encryptKey := m.Sum(nil)
txtBytes := []byte(txt)
t := bytes.NewBufferString("")
for i, ctr := 0, 0; i < len(txtBytes); i++ {
if ctr == len(encryptKey) {
ctr = 0
}
t.WriteByte(encryptKey[ctr])
t.WriteByte(txtBytes[i] ^ encryptKey[ctr])
ctr++
}
return string(keyEd(t.Bytes(), key))
}
func Decrypt(txt, key string) string {
b := keyEd([]byte(txt), key)
t := bytes.NewBufferString("")
for i := 0; i < len(b); i += 2 {
t.WriteByte(b[i] ^ b[i+1])
}
return t.String()
}
func main() {
s := "123456789fefefef3r33你好"
e := Encrypt(s, "salt")
d := Decrypt(e, "salt")
fmt.Println(d)
}