github.heygears.com/openimsdk/tools@v0.0.49/utils/encrypt/encryption_test.go (about) 1 // Copyright © 2023 OpenIM. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package encrypt 15 16 import ( 17 "testing" 18 ) 19 20 func TestMd5(t *testing.T) { 21 // Test without salt 22 expectedWithoutSalt := "098f6bcd4621d373cade4e832627b4f6" // MD5 for "test" 23 resultWithoutSalt := Md5("test") 24 if resultWithoutSalt != expectedWithoutSalt { 25 t.Errorf("Md5 without salt = %v, want %v", resultWithoutSalt, expectedWithoutSalt) 26 } 27 28 // Test with salt 29 expectedWithSalt := Md5("test", "salt") // Generate an expected value with a known salt 30 if len(expectedWithSalt) == 0 { 31 t.Errorf("Md5 with salt generated an empty string") 32 } 33 } 34 35 func TestAesEncryptionDecryption(t *testing.T) { 36 key := []byte("1234567890123456") // AES-128; keys are 16, 24, or 32 bytes for AES-128, AES-192, AES-256 37 originalText := "Hello, World!" 38 39 // Encrypt 40 encrypted, err := AesEncrypt([]byte(originalText), key) 41 if err != nil { 42 t.Fatalf("AesEncrypt error: %v", err) 43 } 44 if len(encrypted) == 0 { 45 t.Errorf("AesEncrypt returned empty data") 46 } 47 48 // Decrypt 49 decrypted, err := AesDecrypt(encrypted, key) 50 if err != nil { 51 t.Fatalf("AesDecrypt error: %v", err) 52 } 53 if string(decrypted) != originalText { 54 t.Errorf("AesDecrypt = %v, want %v", string(decrypted), originalText) 55 } 56 }