Skip to content

Commit 9bc6ae1

Browse files
committed
chore: add some comments
1 parent a2d5aef commit 9bc6ae1

File tree

10 files changed

+42
-116
lines changed

10 files changed

+42
-116
lines changed

3des.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package openssl
22

33
import "crypto/des"
44

5-
// Des3ECBEncrypt
5+
// Des3ECBEncrypt encrypts data using the ECB mode of the 3DES algorithm.
66
func Des3ECBEncrypt(src, key []byte, padding string) ([]byte, error) {
77
block, err := des.NewTripleDESCipher(key)
88
if err != nil {
@@ -11,7 +11,7 @@ func Des3ECBEncrypt(src, key []byte, padding string) ([]byte, error) {
1111
return ECBEncrypt(block, src, padding)
1212
}
1313

14-
// Des3ECBDecrypt
14+
// Des3ECBDecrypt decrypts data using the ECB mode of the 3DES algorithm.
1515
func Des3ECBDecrypt(src, key []byte, padding string) ([]byte, error) {
1616
block, err := des.NewTripleDESCipher(key)
1717
if err != nil {
@@ -21,7 +21,7 @@ func Des3ECBDecrypt(src, key []byte, padding string) ([]byte, error) {
2121
return ECBDecrypt(block, src, padding)
2222
}
2323

24-
// Des3CBCEncrypt
24+
// Des3CBCEncrypt encrypts data using the CBC mode of the 3DES algorithm.
2525
func Des3CBCEncrypt(src, key, iv []byte, padding string) ([]byte, error) {
2626
block, err := des.NewTripleDESCipher(key)
2727
if err != nil {
@@ -31,7 +31,7 @@ func Des3CBCEncrypt(src, key, iv []byte, padding string) ([]byte, error) {
3131
return CBCEncrypt(block, src, iv, padding)
3232
}
3333

34-
// Des3CBCDecrypt
34+
// Des3CBCDecrypt decrypts data using the CBC mode of the 3DES algorithm.
3535
func Des3CBCDecrypt(src, key, iv []byte, padding string) ([]byte, error) {
3636
block, err := des.NewTripleDESCipher(key)
3737
if err != nil {

aes.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"crypto/cipher"
77
)
88

9-
// AesECBEncrypt
9+
// AesECBEncrypt encrypts data using the ECB mode of the AES algorithm.
1010
func AesECBEncrypt(src, key []byte, padding string) ([]byte, error) {
1111
block, err := AesNewCipher(key)
1212
if err != nil {
@@ -15,7 +15,7 @@ func AesECBEncrypt(src, key []byte, padding string) ([]byte, error) {
1515
return ECBEncrypt(block, src, padding)
1616
}
1717

18-
// AesECBDecrypt
18+
// AesECBDecrypt decrypts data using the ECB mode of the AES algorithm.
1919
func AesECBDecrypt(src, key []byte, padding string) ([]byte, error) {
2020
block, err := AesNewCipher(key)
2121
if err != nil {
@@ -25,7 +25,7 @@ func AesECBDecrypt(src, key []byte, padding string) ([]byte, error) {
2525
return ECBDecrypt(block, src, padding)
2626
}
2727

28-
// AesCBCEncrypt
28+
// AesCBCEncrypt encrypts data using the CBC mode of the AES algorithm.
2929
func AesCBCEncrypt(src, key, iv []byte, padding string) ([]byte, error) {
3030
block, err := AesNewCipher(key)
3131
if err != nil {
@@ -35,7 +35,7 @@ func AesCBCEncrypt(src, key, iv []byte, padding string) ([]byte, error) {
3535
return CBCEncrypt(block, src, iv, padding)
3636
}
3737

38-
// AesCBCDecrypt
38+
// AesCBCDecrypt decrypts data using the CBC mode of the AES algorithm.
3939
func AesCBCDecrypt(src, key, iv []byte, padding string) ([]byte, error) {
4040
block, err := AesNewCipher(key)
4141
if err != nil {
@@ -45,13 +45,12 @@ func AesCBCDecrypt(src, key, iv []byte, padding string) ([]byte, error) {
4545
return CBCDecrypt(block, src, iv, padding)
4646
}
4747

48-
// AesNewCipher creates and returns a new AES cipher.Block.
49-
// it will automatically pad the length of the key.
48+
// AesNewCipher creates and returns a new AES cipher block. Automatically pads the key length.
5049
func AesNewCipher(key []byte) (cipher.Block, error) {
5150
return aes.NewCipher(aesKeyPending(key))
5251
}
5352

54-
// aesKeyPending The length of the key can be 16/24/32 characters (128/192/256 bits)
53+
// aesKeyPending ensures the key length is 16, 24, or 32 bytes (128, 192, or 256 bits).
5554
func aesKeyPending(key []byte) []byte {
5655
k := len(key)
5756
count := 0

cbc.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"crypto/cipher"
66
)
77

8-
// CBCEncrypt
8+
// CBCEncrypt encrypts data using the CBC (Cipher Block Chaining) mode.
99
func CBCEncrypt(block cipher.Block, src, iv []byte, padding string) ([]byte, error) {
1010
blockSize := block.BlockSize()
1111
src = Padding(padding, src, blockSize)
@@ -24,7 +24,7 @@ func CBCEncrypt(block cipher.Block, src, iv []byte, padding string) ([]byte, err
2424
return encryptData, nil
2525
}
2626

27-
// CBCDecrypt
27+
// CBCDecrypt decrypts data using the CBC (Cipher Block Chaining) mode.
2828
func CBCDecrypt(block cipher.Block, src, iv []byte, padding string) ([]byte, error) {
2929

3030
dst := make([]byte, len(src))
@@ -41,7 +41,7 @@ func CBCDecrypt(block cipher.Block, src, iv []byte, padding string) ([]byte, err
4141
return UnPadding(padding, dst)
4242
}
4343

44-
// cbcIVPending auto pad length to block size
44+
// cbcIVPending automatically pads or truncates the IV to match the block size.
4545
func cbcIVPending(iv []byte, blockSize int) []byte {
4646
k := len(iv)
4747
if k < blockSize {

covprofile

Lines changed: 0 additions & 89 deletions
This file was deleted.

des.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"crypto/des"
77
)
88

9-
// DesECBEncrypt
9+
// DesECBEncrypt encrypts data using the ECB mode of the DES algorithm.
1010
func DesECBEncrypt(src, key []byte, padding string) ([]byte, error) {
1111
block, err := DesNewCipher(key)
1212
if err != nil {
@@ -15,7 +15,7 @@ func DesECBEncrypt(src, key []byte, padding string) ([]byte, error) {
1515
return ECBEncrypt(block, src, padding)
1616
}
1717

18-
// DesECBDecrypt
18+
// DesECBDecrypt decrypts data using the ECB mode of the DES algorithm.
1919
func DesECBDecrypt(src, key []byte, padding string) ([]byte, error) {
2020
block, err := DesNewCipher(key)
2121
if err != nil {
@@ -25,7 +25,7 @@ func DesECBDecrypt(src, key []byte, padding string) ([]byte, error) {
2525
return ECBDecrypt(block, src, padding)
2626
}
2727

28-
// DesCBCEncrypt
28+
// DesCBCEncrypt encrypts data using the CBC mode of the DES algorithm.
2929
func DesCBCEncrypt(src, key, iv []byte, padding string) ([]byte, error) {
3030
block, err := DesNewCipher(key)
3131
if err != nil {
@@ -35,7 +35,7 @@ func DesCBCEncrypt(src, key, iv []byte, padding string) ([]byte, error) {
3535
return CBCEncrypt(block, src, iv, padding)
3636
}
3737

38-
// DesCBCDecrypt
38+
// DesCBCDecrypt decrypts data using the CBC mode of the DES algorithm.
3939
func DesCBCDecrypt(src, key, iv []byte, padding string) ([]byte, error) {
4040
block, err := DesNewCipher(key)
4141
if err != nil {
@@ -45,7 +45,7 @@ func DesCBCDecrypt(src, key, iv []byte, padding string) ([]byte, error) {
4545
return CBCDecrypt(block, src, iv, padding)
4646
}
4747

48-
// DesNewCipher creates and returns a new DES cipher.Block.
48+
// DesNewCipher creates and returns a new DES cipher block, adjusting the key length if necessary.
4949
func DesNewCipher(key []byte) (cipher.Block, error) {
5050
if len(key) < 8 {
5151
key = append(key, bytes.Repeat([]byte{0}, 8-len(key))...)

ecb.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"crypto/cipher"
55
)
66

7+
// Encrypts data using the ECB (Electronic Codebook) mode.
78
func ECBEncrypt(block cipher.Block, src []byte, padding string) ([]byte, error) {
89
blockSize := block.BlockSize()
910
src = Padding(padding, src, blockSize)
@@ -16,6 +17,7 @@ func ECBEncrypt(block cipher.Block, src []byte, padding string) ([]byte, error)
1617
return encryptData, nil
1718
}
1819

20+
// Decrypts data using the ECB (Electronic Codebook) mode.
1921
func ECBDecrypt(block cipher.Block, src []byte, padding string) ([]byte, error) {
2022
dst := make([]byte, len(src))
2123

@@ -30,6 +32,7 @@ type ecb struct {
3032
blockSize int
3133
}
3234

35+
// Creates a new ECB struct.
3336
func newECB(b cipher.Block) *ecb {
3437
return &ecb{
3538
b: b,
@@ -47,6 +50,7 @@ func NewECBEncrypter(b cipher.Block) cipher.BlockMode {
4750

4851
func (x *ecbEncrypter) BlockSize() int { return x.blockSize }
4952

53+
// Encrypts a series of blocks using the ECB mode.
5054
func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
5155
if len(src)%x.blockSize != 0 {
5256
panic("crypto/cipher: input not full blocks")
@@ -71,6 +75,7 @@ func NewECBDecrypter(b cipher.Block) cipher.BlockMode {
7175

7276
func (x *ecbDecrypter) BlockSize() int { return x.blockSize }
7377

78+
// Decrypts a series of blocks using the ECB mode.
7479
func (x *ecbDecrypter) CryptBlocks(dst, src []byte) {
7580
if len(src)%x.blockSize != 0 {
7681
panic("crypto/cipher: input not full blocks")

key.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"crypto/sha1"
55
)
66

7+
// Generates a key based on the input data and specified block size.
78
func KeyGenerator(src []byte, blockSize int) []byte {
89
hashs := SHA1(SHA1(src))
910
maxLen := len(hashs)
@@ -14,6 +15,7 @@ func KeyGenerator(src []byte, blockSize int) []byte {
1415
return hashs[0:blockSize]
1516
}
1617

18+
// Computes the SHA-1 hash of the input data.
1719
func SHA1(data []byte) []byte {
1820
h := sha1.New()
1921
_, _ = h.Write(data)

md5.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import (
55
"encoding/hex"
66
)
77

8-
// Md5 Calculate the md5 hash of a string
8+
// Calculates the MD5 hash of a given string.
99
func Md5(str string) []byte {
1010
h := md5.New()
1111
_, _ = h.Write([]byte(str))
1212
return h.Sum(nil)
1313
}
1414

15-
// Md5ToString Calculate the md5 hash of a string, return hex string
15+
// Calculates the MD5 hash of a given string and returns the result as a hexadecimal string.
1616
func Md5ToString(str string) string {
1717
return hex.EncodeToString(Md5(str))
1818
}

padding.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import (
55
"errors"
66
)
77

8+
// Error indicating an issue during the unpadding process.
89
var ErrUnPadding = errors.New("UnPadding error")
910

1011
const PKCS5_PADDING = "PKCS5"
1112
const PKCS7_PADDING = "PKCS7"
1213
const ZEROS_PADDING = "ZEROS"
1314

15+
// Applies the specified padding scheme to the input data.
1416
func Padding(padding string, src []byte, blockSize int) []byte {
1517
switch padding {
1618
case PKCS5_PADDING:
@@ -23,6 +25,7 @@ func Padding(padding string, src []byte, blockSize int) []byte {
2325
return src
2426
}
2527

28+
// Removes the specified padding from the input data.
2629
func UnPadding(padding string, src []byte) ([]byte, error) {
2730
switch padding {
2831
case PKCS5_PADDING:
@@ -35,20 +38,24 @@ func UnPadding(padding string, src []byte) ([]byte, error) {
3538
return src, nil
3639
}
3740

41+
// Applies PKCS5 padding to the input data. In practice, it uses PKCS7 padding.
3842
func PKCS5Padding(src []byte, blockSize int) []byte {
3943
return PKCS7Padding(src, blockSize)
4044
}
4145

46+
// Removes PKCS5 padding from the input data. In practice, it uses PKCS7 unpadding.
4247
func PKCS5Unpadding(src []byte) ([]byte, error) {
4348
return PKCS7UnPadding(src)
4449
}
4550

51+
// Applies PKCS7 padding to the input data.
4652
func PKCS7Padding(src []byte, blockSize int) []byte {
4753
padding := blockSize - len(src)%blockSize
4854
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
4955
return append(src, padtext...)
5056
}
5157

58+
// Removes PKCS7 padding from the input data.
5259
func PKCS7UnPadding(src []byte) ([]byte, error) {
5360
length := len(src)
5461
if length == 0 {
@@ -61,6 +68,7 @@ func PKCS7UnPadding(src []byte) ([]byte, error) {
6168
return src[:(length - unpadding)], nil
6269
}
6370

71+
// Applies zero padding to the input data.
6472
func ZerosPadding(src []byte, blockSize int) []byte {
6573
paddingCount := blockSize - len(src)%blockSize
6674
if paddingCount == 0 {
@@ -70,6 +78,7 @@ func ZerosPadding(src []byte, blockSize int) []byte {
7078
}
7179
}
7280

81+
// Removes zero padding from the input data.
7382
func ZerosUnPadding(src []byte) ([]byte, error) {
7483
for i := len(src) - 1; ; i-- {
7584
if src[i] != 0 {

0 commit comments

Comments
 (0)