44 lines
1.4 KiB
Go
44 lines
1.4 KiB
Go
package logcodec
|
|
|
|
import (
|
|
"crypto/cipher"
|
|
"crypto/des"
|
|
"encoding/base64"
|
|
"testing"
|
|
)
|
|
|
|
func TestDecryptLineWithKnownPolicyRoundTrip(t *testing.T) {
|
|
plaintext := "--------------------------------------------------------------------------------------------------------------------------------------------\r\n2026/7/7 15:30:07\r\n<message id=\"msg-1\" from=\"sender@imopenfire1-lanzhou/imp_pc_4.1.2.6842\" to=\"receiver@imopenfire1-lanzhou\" type=\"chat\"><body>redacted</body></message>"
|
|
ciphertext := encryptLineForTest(t, plaintext)
|
|
|
|
got, err := DecryptLine(ciphertext)
|
|
if err != nil {
|
|
t.Fatalf("DecryptLine returned error: %v", err)
|
|
}
|
|
if got != plaintext {
|
|
t.Fatalf("DecryptLine() = %q, want %q", got, plaintext)
|
|
}
|
|
}
|
|
|
|
func encryptLineForTest(t *testing.T, plaintext string) string {
|
|
t.Helper()
|
|
block, err := des.NewCipher([]byte("hyhccdtm"))
|
|
if err != nil {
|
|
t.Fatalf("NewCipher: %v", err)
|
|
}
|
|
plain := padPKCS7ForTest([]byte(plaintext), des.BlockSize)
|
|
ciphertext := make([]byte, len(plain))
|
|
iv := []byte{0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}
|
|
cipher.NewCBCEncrypter(block, iv).CryptBlocks(ciphertext, plain)
|
|
return base64.StdEncoding.EncodeToString(ciphertext)
|
|
}
|
|
|
|
func padPKCS7ForTest(data []byte, blockSize int) []byte {
|
|
pad := blockSize - len(data)%blockSize
|
|
out := append([]byte(nil), data...)
|
|
for i := 0; i < pad; i++ {
|
|
out = append(out, byte(pad))
|
|
}
|
|
return out
|
|
}
|