This repository has been archived by the owner on Feb 6, 2021. It is now read-only.
/
line_crypto.go
76 lines (67 loc) · 1.7 KB
/
line_crypto.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
package line_login
import (
"github.com/5hields/line-login/linethrift"
"crypto/aes"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"encoding/hex"
"fmt"
"log"
"math/big"
)
func setPassword(msg string) string {
sep := rune(len(msg))
return string(sep)
}
func encryptCredential(email, pwd string, key *linethrift.RSAKey) string {
rsaKey := rsaKeyGen(key)
msg := []byte(setPassword(key.SessionKey) + key.SessionKey + setPassword(email) + email + setPassword(pwd) + pwd)
cipherText, err := rsa.EncryptPKCS1v15(rand.Reader, &rsaKey, msg)
if err != nil {
log.Fatal(err)
}
return hex.EncodeToString(cipherText)
}
func xor(data []byte) (r []byte) {
length := len(data) / 2
r = make([]byte, length)
for i := 0; i < length; i++ {
r[i] = data[i] ^ data[length+i]
}
return
}
func getSHA256Sum(data ...[]byte) (r []byte) {
sha := sha256.New()
for _, update := range data {
sha.Write(update)
}
r = sha.Sum(nil)
return
}
func aesECBEncrypt(aesKey, plainText []byte) (cipherText []byte, err error) {
cipherText = make([]byte, len(plainText))
block, err := aes.NewCipher(aesKey)
ecb := newECBEncrypter(block)
ecb.CryptBlocks(cipherText, plainText)
return
}
func getHashKeyChain(sharedSecret, encKeyChain []byte) []byte {
hashKey := getSHA256Sum(sharedSecret, []byte("Key"))
data := xor(getSHA256Sum(encKeyChain))
encKey, _ := aesECBEncrypt(hashKey, data)
return encKey
}
func createSecret(data []byte) (string, []byte) {
pin, _ := createPinCode()
enc, _ := aesECBEncrypt(getSHA256Sum([]byte(pin)), data)
return pin, enc
}
func createPinCode() (string, error) {
pin, err := rand.Int(rand.Reader, big.NewInt(999999))
if err != nil {
return "", err
}
// padding
return fmt.Sprintf("%06d", pin), nil
}