-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yumaojun03
committed
Nov 30, 2024
1 parent
fea8488
commit ca99f7e
Showing
9 changed files
with
226 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package cbc | ||
|
||
type DATA_ENCODE_TYPE int | ||
|
||
const ( | ||
DATA_ENCODE_TYPE_HEX DATA_ENCODE_TYPE = iota | ||
DATA_ENCODE_TYPE_BASE64 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package cbc | ||
|
||
import ( | ||
"bytes" | ||
"crypto/rand" | ||
"crypto/sha1" | ||
) | ||
|
||
// aesCBCEncrypt aes加密,填充秘钥key的16位,24,32分别对应AES-128, AES-192, or AES-256. | ||
type AES_KEY_LEN int | ||
|
||
const ( | ||
AES_KEY_LEN_16 AES_KEY_LEN = 16 | ||
AES_KEY_LEN_24 AES_KEY_LEN = 24 | ||
AES_KEY_LEN_32 AES_KEY_LEN = 32 | ||
) | ||
|
||
// 采用hmac进行2次hash, 取32位, 这样对key没有长度要求 | ||
func GenKeyBySHA1Hash2(key string, keyLen AES_KEY_LEN) []byte { | ||
h := sha1.New() | ||
h.Write([]byte(key)) | ||
hashData := h.Sum(nil) | ||
keyBuffer := bytes.NewBuffer(hashData) | ||
|
||
h.Reset() | ||
h.Write(hashData) | ||
keyBuffer.Write(h.Sum(nil)) | ||
return keyBuffer.Bytes()[:keyLen] | ||
} | ||
|
||
func GenRandomKey(keyLen AES_KEY_LEN) ([]byte, error) { | ||
key := make([]byte, keyLen) | ||
if _, err := rand.Read(key); err != nil { | ||
return nil, err | ||
} | ||
|
||
return key, nil | ||
} | ||
|
||
func MustGenRandomKey(keyLen AES_KEY_LEN) []byte { | ||
v, _ := GenRandomKey(keyLen) | ||
return v | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cbc_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/infraboard/mcube/v2/crypto/cbc" | ||
) | ||
|
||
func TestGenKeyBySHA1Hash2(t *testing.T) { | ||
aesCihperKey := cbc.GenKeyBySHA1Hash2("test", cbc.AES_KEY_LEN_32) | ||
t.Log(aesCihperKey) | ||
} | ||
|
||
func TestMustGenRandomKey(t *testing.T) { | ||
aesCihperKey := cbc.MustGenRandomKey(cbc.AES_KEY_LEN_32) | ||
t.Log(aesCihperKey) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package cbc | ||
|
||
import "bytes" | ||
|
||
// PKCS7 Padding 和 PKCS5 Padding 都是用于对称加密中填充数据的方式,但它们之间有一些细微的区别: | ||
// 填充块大小: | ||
// PKCS5:只适用于块大小为 8 字节的加密算法(如 DES)。它的填充方式是将填充字节的值设为填充的字节数。 | ||
// PKCS7:可以用于块大小为 1 到 255 字节的加密算法(如 AES,块大小为 16 字节)。它的填充方式与 PKCS#5 类似,但适用于更广泛的块大小。 | ||
|
||
// PKCS7Padding 对数据进行 PKCS#7 填充 | ||
func PKCS7Padding(ciphertext []byte, blockSize int) []byte { | ||
padding := blockSize - len(ciphertext)%blockSize | ||
padtext := bytes.Repeat([]byte{byte(padding)}, padding) | ||
return append(ciphertext, padtext...) | ||
} | ||
|
||
// PKCS7Unpadding 对数据进行 PKCS#7 去填充 | ||
func PKCS7Unpadding(origData []byte) []byte { | ||
length := len(origData) | ||
unpadding := int(origData[length-1]) | ||
return origData[:(length - unpadding)] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package cbc_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/infraboard/mcube/v2/crypto/cbc" | ||
) | ||
|
||
func TestPKCS7Padding(t *testing.T) { | ||
data := []byte("Hello World!") | ||
blockSize := 16 // PKCS#7 适用于 1 到 255 字节的块,这里以 16 字节为例 | ||
|
||
// 填充 | ||
paddedData := cbc.PKCS7Padding(data, blockSize) | ||
fmt.Printf("Padded Data (PKCS#7): %x\n", paddedData) | ||
|
||
// 去填充 | ||
unpaddedData := cbc.PKCS7Unpadding(paddedData) | ||
fmt.Printf("Unpadded Data: %x, %s\n", unpaddedData, unpaddedData) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<mxfile host="65bd71144e"> | ||
<diagram id="BEsFAiTLR5EbDN4ZmuPq" name="第 1 页"> | ||
<mxGraphModel dx="1181" dy="657" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0"> | ||
<root> | ||
<mxCell id="0"/> | ||
<mxCell id="1" parent="0"/> | ||
<mxCell id="2" value="app" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> | ||
<mxGeometry x="70" y="180" width="170" height="100" as="geometry"/> | ||
</mxCell> | ||
<mxCell id="3" value="" style="shape=cylinder3;whiteSpace=wrap;html=1;boundedLbl=1;backgroundOutline=1;size=15;" vertex="1" parent="1"> | ||
<mxGeometry x="100" y="390" width="130" height="60" as="geometry"/> | ||
</mxCell> | ||
<mxCell id="4" value="app" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> | ||
<mxGeometry x="80" y="190" width="170" height="100" as="geometry"/> | ||
</mxCell> | ||
<mxCell id="5" value="app<br><br>" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> | ||
<mxGeometry x="90" y="200" width="170" height="100" as="geometry"/> | ||
</mxCell> | ||
<mxCell id="7" value="" style="shape=cylinder3;whiteSpace=wrap;html=1;boundedLbl=1;backgroundOutline=1;size=15;" vertex="1" parent="1"> | ||
<mxGeometry x="610" y="420" width="130" height="60" as="geometry"/> | ||
</mxCell> | ||
<mxCell id="9" value="app" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> | ||
<mxGeometry x="570" y="240" width="170" height="100" as="geometry"/> | ||
</mxCell> | ||
<mxCell id="10" value="app" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> | ||
<mxGeometry x="580" y="250" width="170" height="100" as="geometry"/> | ||
</mxCell> | ||
<mxCell id="22" style="edgeStyle=orthogonalEdgeStyle;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;entryPerimeter=0;" edge="1" parent="1" source="11" target="7"> | ||
<mxGeometry relative="1" as="geometry"/> | ||
</mxCell> | ||
<mxCell id="11" value="app<br><br>" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> | ||
<mxGeometry x="590" y="260" width="170" height="100" as="geometry"/> | ||
</mxCell> | ||
<mxCell id="16" value="cron" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> | ||
<mxGeometry x="180" y="200" width="80" height="30" as="geometry"/> | ||
</mxCell> | ||
<mxCell id="20" style="edgeStyle=none;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" edge="1" parent="1" source="17" target="19"> | ||
<mxGeometry relative="1" as="geometry"/> | ||
</mxCell> | ||
<mxCell id="17" value="cron" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> | ||
<mxGeometry x="570" y="40" width="170" height="60" as="geometry"/> | ||
</mxCell> | ||
<mxCell id="21" style="edgeStyle=none;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" edge="1" parent="1" source="19" target="9"> | ||
<mxGeometry relative="1" as="geometry"/> | ||
</mxCell> | ||
<mxCell id="19" value="gw" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> | ||
<mxGeometry x="570" y="140" width="170" height="60" as="geometry"/> | ||
</mxCell> | ||
</root> | ||
</mxGraphModel> | ||
</diagram> | ||
</mxfile> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters