package mycrypto

import (
	"fmt"
	"github.com/stretchr/testify/assert"
	"testing"
)

func TestAes256Cbc(t *testing.T) {
	key := "iYWKp8yz11iAzdSrGdYAhaW82Kdw209c"
	iv := "QsHZn2hacbZ5A7PT"
	InitAes256Cbc(key, iv)

	str := "1234567890111213"
	encrypt, err := Aes256CbcEncrypt(str)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("---%+v---\n", encrypt)

	decrypt, err := Aes256CbcDecrypt(encrypt)
	if err != nil {
		fmt.Println(err)
		return
	}

	as := assert.New(t)
	fmt.Printf("%+v\n", as.Equal(str, decrypt))
	fmt.Printf("%s %s\n", str, decrypt)

}

func BenchmarkAes256Cbc(b *testing.B) {
	key := "7zlx6MoTL76B9BiYuO4nytGBUk0HnSJ1"
	iv := "1234567890123456"
	InitAes256Cbc(key, iv)

	for i := 0; i < b.N; i++ {
		str := fmt.Sprintf("440123456789%d10111213", i)
		encrypt, err := Aes256CbcEncrypt(str)
		if err != nil {
			fmt.Println(err)
			return
		}
		//fmt.Printf("%+v%s\n", encrypt, "---")

		_, err = Aes256CbcDecrypt(encrypt)
		if err != nil {
			fmt.Println(err)
			return
		}

	}
}

func BenchmarkAes256CbcCheck(b *testing.B) {
	key := "iYWKp8yz11iAzdSrGdYAhaW82Kdw209c"
	iv := "QsHZn2hacbZ5A7PT"
	InitAes256Cbc(key, iv)
	as := assert.New(b)

	for i := 0; i < b.N; i++ {
		str := fmt.Sprintf("440123456789%d10111213", i)
		encrypt, err := Aes256CbcEncrypt(str)
		if err != nil {
			fmt.Println(err)
			return
		}
		//fmt.Printf("%+v%s\n", encrypt, "---")

		decrypt, err := Aes256CbcDecrypt(encrypt)
		if err != nil {
			fmt.Println(err)
			return
		}

		if !as.Equal(str, decrypt) {
			b.Fail()
		}

	}
}
