-
Notifications
You must be signed in to change notification settings - Fork 21
/
encrypt_padding.go
200 lines (153 loc) · 5.85 KB
/
encrypt_padding.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package crypto
import (
"github.com/deatil/go-cryptobin/tool"
)
type ZeroPaddinger struct {}
// Padding 补码模式 / padding type
func (this ZeroPaddinger) Padding(plainText []byte, blockSize int, opt IOption) []byte {
return tool.NewPadding().ZeroPadding(plainText, blockSize)
}
// UnPadding 补码模式 / unpadding type
func (this ZeroPaddinger) UnPadding(cipherText []byte, opt IOption) ([]byte, error) {
return tool.NewPadding().ZeroUnPadding(cipherText)
}
// ===================
type PKCS5Paddinger struct {}
// Padding 补码模式 / padding type
func (this PKCS5Paddinger) Padding(plainText []byte, blockSize int, opt IOption) []byte {
return tool.NewPadding().PKCS5Padding(plainText)
}
// UnPadding 补码模式 / unpadding type
func (this PKCS5Paddinger) UnPadding(cipherText []byte, opt IOption) ([]byte, error) {
return tool.NewPadding().PKCS5UnPadding(cipherText)
}
// ===================
type PKCS7Paddinger struct {}
// Padding 补码模式 / padding type
func (this PKCS7Paddinger) Padding(plainText []byte, blockSize int, opt IOption) []byte {
return tool.NewPadding().PKCS7Padding(plainText, blockSize)
}
// UnPadding 补码模式 / unpadding type
func (this PKCS7Paddinger) UnPadding(cipherText []byte, opt IOption) ([]byte, error) {
return tool.NewPadding().PKCS7UnPadding(cipherText)
}
// ===================
type X923Paddinger struct {}
// Padding 补码模式 / padding type
func (this X923Paddinger) Padding(plainText []byte, blockSize int, opt IOption) []byte {
return tool.NewPadding().X923Padding(plainText, blockSize)
}
// UnPadding 补码模式 / unpadding type
func (this X923Paddinger) UnPadding(cipherText []byte, opt IOption) ([]byte, error) {
return tool.NewPadding().X923UnPadding(cipherText)
}
// ===================
type ISO10126Paddinger struct {}
// Padding 补码模式 / padding type
func (this ISO10126Paddinger) Padding(plainText []byte, blockSize int, opt IOption) []byte {
return tool.NewPadding().ISO10126Padding(plainText, blockSize)
}
// UnPadding 补码模式 / unpadding type
func (this ISO10126Paddinger) UnPadding(cipherText []byte, opt IOption) ([]byte, error) {
return tool.NewPadding().ISO10126UnPadding(cipherText)
}
// ===================
type ISO7816_4Paddinger struct {}
// Padding 补码模式 / padding type
func (this ISO7816_4Paddinger) Padding(plainText []byte, blockSize int, opt IOption) []byte {
return tool.NewPadding().ISO7816_4Padding(plainText, blockSize)
}
// UnPadding 补码模式 / unpadding type
func (this ISO7816_4Paddinger) UnPadding(cipherText []byte, opt IOption) ([]byte, error) {
return tool.NewPadding().ISO7816_4UnPadding(cipherText)
}
// ===================
type ISO97971Paddinger struct {}
// Padding 补码模式 / padding type
func (this ISO97971Paddinger) Padding(plainText []byte, blockSize int, opt IOption) []byte {
return tool.NewPadding().ISO97971Padding(plainText, blockSize)
}
// UnPadding 补码模式 / unpadding type
func (this ISO97971Paddinger) UnPadding(cipherText []byte, opt IOption) ([]byte, error) {
return tool.NewPadding().ISO97971UnPadding(cipherText)
}
// ===================
type PBOC2Paddinger struct {}
// Padding 补码模式 / padding type
func (this PBOC2Paddinger) Padding(plainText []byte, blockSize int, opt IOption) []byte {
return tool.NewPadding().PBOC2Padding(plainText, blockSize)
}
// UnPadding 补码模式 / unpadding type
func (this PBOC2Paddinger) UnPadding(cipherText []byte, opt IOption) ([]byte, error) {
return tool.NewPadding().PBOC2UnPadding(cipherText)
}
// ===================
type TBCPaddinger struct {}
// Padding 补码模式 / padding type
func (this TBCPaddinger) Padding(plainText []byte, blockSize int, opt IOption) []byte {
return tool.NewPadding().TBCPadding(plainText, blockSize)
}
// UnPadding 补码模式 / unpadding type
func (this TBCPaddinger) UnPadding(cipherText []byte, opt IOption) ([]byte, error) {
return tool.NewPadding().TBCUnPadding(cipherText)
}
// ===================
type PKCS1Paddinger struct {}
// Padding 补码模式 / padding type
func (this PKCS1Paddinger) Padding(plainText []byte, blockSize int, opt IOption) []byte {
bt := "02"
if !opt.Config().Has("pkcs1_padding_bt") {
bt = opt.Config().GetString("pkcs1_padding_bt")
}
return tool.NewPadding().PKCS1Padding(plainText, blockSize, bt)
}
// UnPadding 补码模式 / unpadding type
func (this PKCS1Paddinger) UnPadding(cipherText []byte, opt IOption) ([]byte, error) {
return tool.NewPadding().PKCS1UnPadding(cipherText)
}
// ===================
type NoPaddinger struct {}
// Padding 补码模式 / padding type
func (this NoPaddinger) Padding(plainText []byte, blockSize int, opt IOption) []byte {
return plainText
}
// UnPadding 补码模式 / unpadding type
func (this NoPaddinger) UnPadding(cipherText []byte, opt IOption) ([]byte, error) {
return cipherText, nil
}
// ===================
func init() {
UsePadding.Add(ZeroPadding, func() IPadding {
return ZeroPaddinger{}
})
UsePadding.Add(PKCS5Padding, func() IPadding {
return PKCS5Paddinger{}
})
UsePadding.Add(PKCS7Padding, func() IPadding {
return PKCS7Paddinger{}
})
UsePadding.Add(X923Padding, func() IPadding {
return X923Paddinger{}
})
UsePadding.Add(ISO10126Padding, func() IPadding {
return ISO10126Paddinger{}
})
UsePadding.Add(ISO7816_4Padding, func() IPadding {
return ISO7816_4Paddinger{}
})
UsePadding.Add(ISO97971Padding, func() IPadding {
return ISO97971Paddinger{}
})
UsePadding.Add(PBOC2Padding, func() IPadding {
return PBOC2Paddinger{}
})
UsePadding.Add(TBCPadding, func() IPadding {
return TBCPaddinger{}
})
UsePadding.Add(PKCS1Padding, func() IPadding {
return PKCS1Paddinger{}
})
UsePadding.Add(NoPadding, func() IPadding {
return NoPaddinger{}
})
}