mirror of
https://gitee.com/IrisVega/frp.git
synced 2024-11-01 22:31:29 +08:00
239 lines
3.9 KiB
Go
239 lines
3.9 KiB
Go
|
package kcp
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"crypto/rand"
|
||
|
"hash/crc32"
|
||
|
"io"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestSM4(t *testing.T) {
|
||
|
bc, err := NewSM4BlockCrypt(pass[:16])
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
cryptTest(t, bc)
|
||
|
}
|
||
|
|
||
|
func TestAES(t *testing.T) {
|
||
|
bc, err := NewAESBlockCrypt(pass[:32])
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
cryptTest(t, bc)
|
||
|
}
|
||
|
|
||
|
func TestTEA(t *testing.T) {
|
||
|
bc, err := NewTEABlockCrypt(pass[:16])
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
cryptTest(t, bc)
|
||
|
}
|
||
|
|
||
|
func TestXOR(t *testing.T) {
|
||
|
bc, err := NewSimpleXORBlockCrypt(pass[:32])
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
cryptTest(t, bc)
|
||
|
}
|
||
|
|
||
|
func TestBlowfish(t *testing.T) {
|
||
|
bc, err := NewBlowfishBlockCrypt(pass[:32])
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
cryptTest(t, bc)
|
||
|
}
|
||
|
|
||
|
func TestNone(t *testing.T) {
|
||
|
bc, err := NewNoneBlockCrypt(pass[:32])
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
cryptTest(t, bc)
|
||
|
}
|
||
|
|
||
|
func TestCast5(t *testing.T) {
|
||
|
bc, err := NewCast5BlockCrypt(pass[:16])
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
cryptTest(t, bc)
|
||
|
}
|
||
|
|
||
|
func Test3DES(t *testing.T) {
|
||
|
bc, err := NewTripleDESBlockCrypt(pass[:24])
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
cryptTest(t, bc)
|
||
|
}
|
||
|
|
||
|
func TestTwofish(t *testing.T) {
|
||
|
bc, err := NewTwofishBlockCrypt(pass[:32])
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
cryptTest(t, bc)
|
||
|
}
|
||
|
|
||
|
func TestXTEA(t *testing.T) {
|
||
|
bc, err := NewXTEABlockCrypt(pass[:16])
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
cryptTest(t, bc)
|
||
|
}
|
||
|
|
||
|
func TestSalsa20(t *testing.T) {
|
||
|
bc, err := NewSalsa20BlockCrypt(pass[:32])
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
cryptTest(t, bc)
|
||
|
}
|
||
|
|
||
|
func cryptTest(t *testing.T, bc BlockCrypt) {
|
||
|
data := make([]byte, mtuLimit)
|
||
|
io.ReadFull(rand.Reader, data)
|
||
|
dec := make([]byte, mtuLimit)
|
||
|
enc := make([]byte, mtuLimit)
|
||
|
bc.Encrypt(enc, data)
|
||
|
bc.Decrypt(dec, enc)
|
||
|
if !bytes.Equal(data, dec) {
|
||
|
t.Fail()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func BenchmarkSM4(b *testing.B) {
|
||
|
bc, err := NewSM4BlockCrypt(pass[:16])
|
||
|
if err != nil {
|
||
|
b.Fatal(err)
|
||
|
}
|
||
|
benchCrypt(b, bc)
|
||
|
}
|
||
|
|
||
|
func BenchmarkAES128(b *testing.B) {
|
||
|
bc, err := NewAESBlockCrypt(pass[:16])
|
||
|
if err != nil {
|
||
|
b.Fatal(err)
|
||
|
}
|
||
|
|
||
|
benchCrypt(b, bc)
|
||
|
}
|
||
|
|
||
|
func BenchmarkAES192(b *testing.B) {
|
||
|
bc, err := NewAESBlockCrypt(pass[:24])
|
||
|
if err != nil {
|
||
|
b.Fatal(err)
|
||
|
}
|
||
|
|
||
|
benchCrypt(b, bc)
|
||
|
}
|
||
|
|
||
|
func BenchmarkAES256(b *testing.B) {
|
||
|
bc, err := NewAESBlockCrypt(pass[:32])
|
||
|
if err != nil {
|
||
|
b.Fatal(err)
|
||
|
}
|
||
|
|
||
|
benchCrypt(b, bc)
|
||
|
}
|
||
|
|
||
|
func BenchmarkTEA(b *testing.B) {
|
||
|
bc, err := NewTEABlockCrypt(pass[:16])
|
||
|
if err != nil {
|
||
|
b.Fatal(err)
|
||
|
}
|
||
|
benchCrypt(b, bc)
|
||
|
}
|
||
|
|
||
|
func BenchmarkXOR(b *testing.B) {
|
||
|
bc, err := NewSimpleXORBlockCrypt(pass[:32])
|
||
|
if err != nil {
|
||
|
b.Fatal(err)
|
||
|
}
|
||
|
benchCrypt(b, bc)
|
||
|
}
|
||
|
|
||
|
func BenchmarkBlowfish(b *testing.B) {
|
||
|
bc, err := NewBlowfishBlockCrypt(pass[:32])
|
||
|
if err != nil {
|
||
|
b.Fatal(err)
|
||
|
}
|
||
|
benchCrypt(b, bc)
|
||
|
}
|
||
|
|
||
|
func BenchmarkNone(b *testing.B) {
|
||
|
bc, err := NewNoneBlockCrypt(pass[:32])
|
||
|
if err != nil {
|
||
|
b.Fatal(err)
|
||
|
}
|
||
|
benchCrypt(b, bc)
|
||
|
}
|
||
|
|
||
|
func BenchmarkCast5(b *testing.B) {
|
||
|
bc, err := NewCast5BlockCrypt(pass[:16])
|
||
|
if err != nil {
|
||
|
b.Fatal(err)
|
||
|
}
|
||
|
benchCrypt(b, bc)
|
||
|
}
|
||
|
|
||
|
func Benchmark3DES(b *testing.B) {
|
||
|
bc, err := NewTripleDESBlockCrypt(pass[:24])
|
||
|
if err != nil {
|
||
|
b.Fatal(err)
|
||
|
}
|
||
|
benchCrypt(b, bc)
|
||
|
}
|
||
|
|
||
|
func BenchmarkTwofish(b *testing.B) {
|
||
|
bc, err := NewTwofishBlockCrypt(pass[:32])
|
||
|
if err != nil {
|
||
|
b.Fatal(err)
|
||
|
}
|
||
|
benchCrypt(b, bc)
|
||
|
}
|
||
|
|
||
|
func BenchmarkXTEA(b *testing.B) {
|
||
|
bc, err := NewXTEABlockCrypt(pass[:16])
|
||
|
if err != nil {
|
||
|
b.Fatal(err)
|
||
|
}
|
||
|
benchCrypt(b, bc)
|
||
|
}
|
||
|
|
||
|
func BenchmarkSalsa20(b *testing.B) {
|
||
|
bc, err := NewSalsa20BlockCrypt(pass[:32])
|
||
|
if err != nil {
|
||
|
b.Fatal(err)
|
||
|
}
|
||
|
benchCrypt(b, bc)
|
||
|
}
|
||
|
|
||
|
func benchCrypt(b *testing.B, bc BlockCrypt) {
|
||
|
b.ReportAllocs()
|
||
|
data := make([]byte, mtuLimit)
|
||
|
io.ReadFull(rand.Reader, data)
|
||
|
dec := make([]byte, mtuLimit)
|
||
|
enc := make([]byte, mtuLimit)
|
||
|
|
||
|
for i := 0; i < b.N; i++ {
|
||
|
bc.Encrypt(enc, data)
|
||
|
bc.Decrypt(dec, enc)
|
||
|
}
|
||
|
b.SetBytes(int64(len(enc) * 2))
|
||
|
}
|
||
|
|
||
|
func BenchmarkCRC32(b *testing.B) {
|
||
|
content := make([]byte, 1024)
|
||
|
b.SetBytes(int64(len(content)))
|
||
|
for i := 0; i < b.N; i++ {
|
||
|
crc32.ChecksumIEEE(content)
|
||
|
}
|
||
|
}
|