github.com/minio/madmin-go/v2@v2.2.1/encrypt_test.go (about) 1 // 2 // Copyright (c) 2015-2022 MinIO, Inc. 3 // 4 // This file is part of MinIO Object Storage stack 5 // 6 // This program is free software: you can redistribute it and/or modify 7 // it under the terms of the GNU Affero General Public License as 8 // published by the Free Software Foundation, either version 3 of the 9 // License, or (at your option) any later version. 10 // 11 // This program is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU Affero General Public License for more details. 15 // 16 // You should have received a copy of the GNU Affero General Public License 17 // along with this program. If not, see <http://www.gnu.org/licenses/>. 18 // 19 20 package madmin 21 22 import ( 23 "bytes" 24 "encoding/hex" 25 "fmt" 26 "testing" 27 ) 28 29 var encryptDataTests = []struct { 30 Password string 31 Data []byte 32 }{ 33 {Password: "", Data: nil}, 34 {Password: "", Data: make([]byte, 256)}, 35 {Password: `xPl.8/rhR"Q_1xLt`, Data: make([]byte, 32)}, 36 {Password: "m69?yz4W-!k+7p0", Data: make([]byte, 1024*1024)}, 37 {Password: `7h5oU4$te{;K}fgqlI^]`, Data: make([]byte, 256)}, 38 } 39 40 func TestEncryptData(t *testing.T) { 41 for i, test := range encryptDataTests { 42 i, test := i, test 43 t.Run(fmt.Sprintf("Test-%d", i), func(t *testing.T) { 44 ciphertext, err := EncryptData(test.Password, test.Data) 45 if err != nil { 46 t.Fatalf("Failed to encrypt data: %v", err) 47 } 48 49 plaintext, err := DecryptData(test.Password, bytes.NewReader(ciphertext)) 50 if err != nil { 51 t.Fatalf("Failed to decrypt data: %v", err) 52 } 53 if !bytes.Equal(plaintext, test.Data) { 54 t.Fatal("Decrypt plaintext does not match origin data") 55 } 56 }) 57 } 58 } 59 60 var decryptDataTests = []struct { 61 Password string 62 Data string 63 }{ 64 {Password: "", Data: "828aa81599df0651c0461adb82283e8b89956baee9f6e719947ef9cddc849028001dc9d3ac0938f66b07bacc9751437e1985f8a9763c240e81"}, 65 66 {Password: "", Data: "1793c71df6647860437134073c15688cbb15961dc0758c7ee1225e66e79c724c00d790dba9c671eae89da2c736d858286ac9bd027abacc6443" + 67 "0375cd41b63b67c070c7fba475a8dd66ae65ba905176c48cbe6f734fc74df87343d8ccff54bada4aeb0a04bd021633ebe6c4768e23f5dea142" + 68 "561d4fe3f90ed59d13dc5fb3a585dadec1742325291b9c81692bdd3420b2428127f8195e0ecd9a1c9237712ed67af7339fbbf7ff3ee1c516e1" + 69 "f81e69d933e057b30997e7274a2c9698e07c39f0e8d6818858f34c8191871b5a52bea9061806bd029024bfc1d9c1f230904968d6c9e10fddcb" + 70 "c006ba97356ff243570fd96df07dd6894e215a6b24c4ed730369519289ebd877aff6ccbd2265985e4ab1a2b7930bab9cfb767b97348a639ddf" + 71 "8db81bf5151da7e8f3d9638a1b86eb1dd78cc6a526f10a414c78638f"}, 72 73 {Password: `xPl.8/rhR"Q_1xLt`, Data: "b5c016e93b84b473fc8a37af94936563630c36d6df1841d23a86ee51ca161f9e00ac19116b32f643ff6a56a212b265d8c56" + 74 "195bb0d12ce199e13dfdc5272f80c1564da2c6fc2fa18da91d8062de02af5cdafea491c6f3cae1f"}, 75 76 {Password: `7h5oU4$te{;K}fgqlI^]`, Data: "c58edf7cfd557b6b655de6f48b1a3049d8d049dadb3a7bfa9ac9ccbb5baf37ec00f83086a26f43b7d6bc9075ad0" + 77 "38bf5741f118d502ebe94165e4072ba7f98535d6b1e3b6ae67a98115d146d9b4d90e4df4ae82df9cfa17ed7cd42" + 78 "465181559f7ddf09c98beec521bb4478e0cb73c4e0827af8688ff4e7a07327a10d5a180035e6ddb16d974a85257" + 79 "981cd9e0360a20f7b4d653190267dfb241148f018ae180568042e864b9e1b5bc05425a3abc2b0324f50c72d5679" + 80 "8f924405dfc0f8523f4bb564ed65af8e1b1c82a7a0640552ecf81985d95d0993d99172592ddc1393dfa63e8f0b3" + 81 "d744b2cc4b73384ca4693f0c1aec0e9b00e85f2937e891105d67da8f59c14ca96608e0425c42f9c1e7c2a8b3413" + 82 "e1381784f9cfe01de7c47cea1f8d7a7d88f5d4aca783cf55332b47f957a6b9a65269d7eb606b877b"}, 83 } 84 85 func TestDecryptData(t *testing.T) { 86 for i, test := range decryptDataTests { 87 i, test := i, test 88 t.Run(fmt.Sprintf("Test-%d", i), func(t *testing.T) { 89 ciphertext, err := hex.DecodeString(test.Data) 90 if err != nil { 91 t.Fatalf("Failed to decode ciphertext data: %v", err) 92 } 93 _, err = DecryptData(test.Password, bytes.NewReader(ciphertext)) 94 if err != nil { 95 t.Fatalf("Failed to decrypt data: %v", err) 96 } 97 }) 98 } 99 } 100 101 func TestIsDecrypted(t *testing.T) { 102 for i, test := range decryptDataTests { 103 i, test := i, test 104 t.Run(fmt.Sprintf("Test-%d", i), func(t *testing.T) { 105 ciphertext, err := hex.DecodeString(test.Data) 106 if err != nil { 107 t.Fatalf("Failed to decode ciphertext data: %v", err) 108 } 109 if !IsEncrypted(ciphertext) { 110 t.Fatal("Ciphertext is not encrypted") 111 } 112 }) 113 } 114 }