github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/signer/storage/aes_gcm_storage_test.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 //版权所有2018 Go Ethereum作者 10 //此文件是Go以太坊的一部分。 11 // 12 //Go以太坊是免费软件:您可以重新发布和/或修改它 13 //根据GNU通用公共许可证的条款 14 //自由软件基金会,或者许可证的第3版,或者 15 //(由您选择)任何更高版本。 16 // 17 //Go以太坊的分布希望它会有用, 18 //但没有任何保证;甚至没有 19 //适销性或特定用途的适用性。见 20 //GNU通用公共许可证了解更多详细信息。 21 // 22 //你应该已经收到一份GNU通用公共许可证的副本 23 //一起去以太坊吧。如果没有,请参见<http://www.gnu.org/licenses/>。 24 // 25 package storage 26 27 import ( 28 "bytes" 29 "fmt" 30 "io/ioutil" 31 "testing" 32 33 "github.com/ethereum/go-ethereum/common" 34 "github.com/ethereum/go-ethereum/log" 35 "github.com/mattn/go-colorable" 36 ) 37 38 func TestEncryption(t *testing.T) { 39 //键:=[]字节(“aes256key-32字符1234567890”) 40 //明文:=[]字节(值) 41 key := []byte("AES256Key-32Characters1234567890") 42 plaintext := []byte("exampleplaintext") 43 44 c, iv, err := encrypt(key, plaintext) 45 if err != nil { 46 t.Fatal(err) 47 } 48 fmt.Printf("Ciphertext %x, nonce %x\n", c, iv) 49 50 p, err := decrypt(key, iv, c) 51 if err != nil { 52 t.Fatal(err) 53 } 54 fmt.Printf("Plaintext %v\n", string(p)) 55 if !bytes.Equal(plaintext, p) { 56 t.Errorf("Failed: expected plaintext recovery, got %v expected %v", string(plaintext), string(p)) 57 } 58 } 59 60 func TestFileStorage(t *testing.T) { 61 62 a := map[string]storedCredential{ 63 "secret": { 64 Iv: common.Hex2Bytes("cdb30036279601aeee60f16b"), 65 CipherText: common.Hex2Bytes("f311ac49859d7260c2c464c28ffac122daf6be801d3cfd3edcbde7e00c9ff74f"), 66 }, 67 "secret2": { 68 Iv: common.Hex2Bytes("afb8a7579bf971db9f8ceeed"), 69 CipherText: common.Hex2Bytes("2df87baf86b5073ef1f03e3cc738de75b511400f5465bb0ddeacf47ae4dc267d"), 70 }, 71 } 72 d, err := ioutil.TempDir("", "eth-encrypted-storage-test") 73 if err != nil { 74 t.Fatal(err) 75 } 76 stored := &AESEncryptedStorage{ 77 filename: fmt.Sprintf("%v/vault.json", d), 78 key: []byte("AES256Key-32Characters1234567890"), 79 } 80 stored.writeEncryptedStorage(a) 81 read := &AESEncryptedStorage{ 82 filename: fmt.Sprintf("%v/vault.json", d), 83 key: []byte("AES256Key-32Characters1234567890"), 84 } 85 creds, err := read.readEncryptedStorage() 86 if err != nil { 87 t.Fatal(err) 88 } 89 for k, v := range a { 90 if v2, exist := creds[k]; !exist { 91 t.Errorf("Missing entry %v", k) 92 } else { 93 if !bytes.Equal(v.CipherText, v2.CipherText) { 94 t.Errorf("Wrong ciphertext, expected %x got %x", v.CipherText, v2.CipherText) 95 } 96 if !bytes.Equal(v.Iv, v2.Iv) { 97 t.Errorf("Wrong iv") 98 } 99 } 100 } 101 } 102 func TestEnd2End(t *testing.T) { 103 log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(3), log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(true)))) 104 105 d, err := ioutil.TempDir("", "eth-encrypted-storage-test") 106 if err != nil { 107 t.Fatal(err) 108 } 109 110 s1 := &AESEncryptedStorage{ 111 filename: fmt.Sprintf("%v/vault.json", d), 112 key: []byte("AES256Key-32Characters1234567890"), 113 } 114 s2 := &AESEncryptedStorage{ 115 filename: fmt.Sprintf("%v/vault.json", d), 116 key: []byte("AES256Key-32Characters1234567890"), 117 } 118 119 s1.Put("bazonk", "foobar") 120 if v := s2.Get("bazonk"); v != "foobar" { 121 t.Errorf("Expected bazonk->foobar, got '%v'", v) 122 } 123 }