github.com/iDigitalFlame/xmt@v0.5.4/data/crypto/crypto_test.go (about) 1 // Copyright (C) 2020 - 2023 iDigitalFlame 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU General Public License as published by 5 // the Free Software Foundation, either version 3 of the License, or 6 // any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU General Public License for more details. 12 // 13 // You should have received a copy of the GNU General Public License 14 // along with this program. If not, see <https://www.gnu.org/licenses/>. 15 // 16 17 package crypto 18 19 import ( 20 "bytes" 21 "crypto/aes" 22 "testing" 23 24 "github.com/iDigitalFlame/xmt/util" 25 ) 26 27 func TestXOR(t *testing.T) { 28 var ( 29 x = make(XOR, 32) 30 i = make([]byte, 32) 31 w bytes.Buffer 32 ) 33 util.Rand.Read(x) 34 util.Rand.Read(i) 35 z, err := NewBlockWriter(x, i, &w) 36 if err != nil { 37 t.Fatalf("TestXOR(): Encrypter failed with error: %s!", err.Error()) 38 } 39 if _, err = z.Write([]byte("hello there")); err != nil { 40 t.Fatalf("TestXOR(): Write failed with error: %s!", err.Error()) 41 } 42 if err = z.Close(); err != nil { 43 t.Fatalf("TestXOR(): Close failed with error: %s!", err.Error()) 44 } 45 r, err := NewBlockReader(x, i, bytes.NewReader(w.Bytes())) 46 if err != nil { 47 t.Fatalf("TestXOR(): Encrypter failed with error: %s!", err.Error()) 48 } 49 o := make([]byte, 11) 50 if _, err := r.Read(o); err != nil { 51 t.Fatalf("TestXOR(): Read failed with error: %s!", err.Error()) 52 } 53 if string(o) != "hello there" { 54 t.Fatalf(`TestXOR(): Result output "%s" did not match "hello there"!`, o) 55 } 56 } 57 func TestCBK(t *testing.T) { 58 var ( 59 c, _ = NewCBKEx(0x90, 128, nil) 60 v, _ = NewCBKSource(c.A, c.B, c.C, c.D, 128) 61 b bytes.Buffer 62 w = NewCBKWriter(c, &b) 63 ) 64 if _, err := w.Write([]byte("hello there")); err != nil { 65 t.Fatalf("TestCBK(): Write failed with error: %s!", err.Error()) 66 } 67 if err := w.Close(); err != nil { 68 t.Fatalf("TestCBK(): Close failed with error: %s!", err.Error()) 69 } 70 var ( 71 r = NewCBKReader(v, bytes.NewReader(b.Bytes())) 72 o = make([]byte, 11) 73 ) 74 if _, err := r.Read(o); err != nil { 75 t.Fatalf("TestCBK(): Read failed with error: %s!", err.Error()) 76 } 77 if string(o) != "hello there" { 78 t.Fatalf(`TestCBK(): Result output "%s" did not match "hello there"!`, o) 79 } 80 } 81 func TestAES(t *testing.T) { 82 k := make([]byte, 32) 83 util.Rand.Read(k) 84 b, err := aes.NewCipher(k) 85 if err != nil { 86 t.Fatalf("TestAES(): NewCipher failed with error: %s!", err.Error()) 87 } 88 var ( 89 i = make([]byte, 16) 90 w bytes.Buffer 91 ) 92 util.Rand.Read(i) 93 z, err := NewBlockWriter(b, i, &w) 94 if err != nil { 95 t.Fatalf("TestAES(): Encrypter failed with error: %s!", err.Error()) 96 } 97 if _, err = z.Write([]byte("hello there")); err != nil { 98 t.Fatalf("TestAES(): Write failed with error: %s!", err.Error()) 99 } 100 if err = z.Close(); err != nil { 101 t.Fatalf("TestAES():Close failed with error: %s!", err.Error()) 102 } 103 r, err := NewBlockReader(b, i, bytes.NewReader(w.Bytes())) 104 if err != nil { 105 t.Fatalf("TestAES(): Encrypter failed with error: %s!", err.Error()) 106 } 107 o := make([]byte, 11) 108 if _, err := r.Read(o); err != nil { 109 t.Fatalf("TestAES():Read failed with error: %s!", err.Error()) 110 } 111 if string(o) != "hello there" { 112 t.Fatalf(`TestAES(): Result output "%s" did not match "hello there"!`, o) 113 } 114 }