github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/xcrypt/crypt_test.go (about) 1 package xcrypt 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 ) 8 9 func TestAes(t *testing.T) { 10 token := []byte("12312312904812903812985qs90127489121314523") 11 iv := token 12 encrypted, err := AesEncryptRaw([]byte("hello"), token, iv) 13 require.Nil(t, err, "err must be nil") 14 decrypted, err := AesDecryptRaw(encrypted, token, iv) 15 require.Nil(t, err, "err must be nil") 16 require.EqualValues(t, "hello", decrypted) 17 } 18 19 func TestBCrypt(t *testing.T) { 20 encoded, err := BCryptHash([]byte("Hello,world")) 21 require.Nil(t, err, "bcrypt hash must success") 22 ok := BCryptValidate([]byte("Hello,world"), encoded) 23 require.True(t, ok, "validate must be successful") 24 } 25 26 //BenchmarkBCrypt 27 //BenchmarkBCrypt/hash 28 //BenchmarkBCrypt/hash-12 19 56413888 ns/op 5277 B/op 11 allocs/op 29 //BenchmarkBCrypt/validate 30 //BenchmarkBCrypt/validate-12 20 56294324 ns/op 5280 B/op 15 allocs/op 31 func BenchmarkBCrypt(b *testing.B) { 32 b.Run("hash", func(b *testing.B) { 33 b.ResetTimer() 34 b.ReportAllocs() 35 for i := 0; i < b.N; i++ { 36 _, _ = BCryptHash([]byte("Hello,world12345")) 37 } 38 }) 39 40 b.Run("validate", func(b *testing.B) { 41 encoded, _ := BCryptHash([]byte("Hello,world")) 42 b.ResetTimer() 43 b.ReportAllocs() 44 for i := 0; i < b.N; i++ { 45 BCryptValidate([]byte("Hello,world12345"), encoded) 46 } 47 }) 48 }