-
Notifications
You must be signed in to change notification settings - Fork 0
/
cipher.go
84 lines (70 loc) · 2.04 KB
/
cipher.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
package lib
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"errors"
"fmt"
"github.com/jxskiss/base62"
"io"
)
func MD5(s string) string {
h := md5.New()
io.WriteString(h, s)
return fmt.Sprintf("%x", h.Sum(nil))
}
func PKCS7Padding(plaintext string, blockSize int) []byte {
padding := blockSize - (len(plaintext) % blockSize)
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
var buffer bytes.Buffer
buffer.WriteString(plaintext)
buffer.Write(padtext)
return buffer.Bytes()
}
func PKCS7UnPadding(plaintext []byte, blockSize int) ([]byte, error) {
plaintextLength := len(plaintext)
if nil == plaintext || plaintextLength == 0 {
return nil, errors.New("pKCS7Unpadding error nil or zero")
}
if plaintextLength%blockSize != 0 {
return nil, errors.New("pKCS7Unpadding text not a multiple of the block size")
}
paddingLength := int(plaintext[plaintextLength-1])
return plaintext[:plaintextLength-paddingLength], nil
}
func AesCbcEncrypt(src, key []byte) []byte {
block, _ := aes.NewCipher(key)
src = PKCS7Padding(string(src), block.BlockSize())
cipherText := make([]byte, len(src))
mode := cipher.NewCBCEncrypter(block, key)
mode.CryptBlocks(cipherText, src)
return cipherText
}
func AesCbcDecrypt(src, key []byte) []byte {
block, _ := aes.NewCipher(key)
dst := make([]byte, len(src))
mode := cipher.NewCBCDecrypter(block, key)
mode.CryptBlocks(dst, src)
b, _ := PKCS7UnPadding(dst, block.BlockSize())
return b
}
//----------------------------------------------------------------------------------------------------------
// 用于数字转短串
type B62Encoder struct {
encoding *base62.Encoding
}
func (do *B62Encoder) Encode(id int64) string {
b := do.encoding.FormatInt(id)
b = PKCS7Padding(BytesToStr(b), 6)
return do.encoding.EncodeToString(b)
}
func (do *B62Encoder) Decode(s string) int64 {
b, _ := do.encoding.DecodeString(s)
raw, _ := PKCS7UnPadding(b, 6)
id, _ := do.encoding.ParseInt(raw)
return id
}
func NewB62Encoder(s string) *B62Encoder {
return &B62Encoder{encoding: base62.NewEncoding(s)}
}