forked from neo-project/neo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUT_Cryptography_Helper.cs
176 lines (162 loc) · 6.67 KB
/
UT_Cryptography_Helper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Cryptography;
using Neo.IO;
using Neo.Network.P2P.Payloads;
using Neo.SmartContract;
using Neo.Wallets;
using Neo.Wallets.NEP6;
using System;
using System.Linq;
using System.Security;
using System.Text;
namespace Neo.UnitTests.Cryptography
{
[TestClass]
public class UT_Cryptography_Helper
{
[TestMethod]
public void TestBase58CheckDecode()
{
string input = "3vQB7B6MrGQZaxCuFg4oh";
byte[] result = input.Base58CheckDecode();
byte[] helloWorld = { 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100 };
result.Should().Equal(helloWorld);
input = "3v";
Action action = () => input.Base58CheckDecode();
action.Should().Throw<FormatException>();
input = "3vQB7B6MrGQZaxCuFg4og";
action = () => input.Base58CheckDecode();
action.Should().Throw<FormatException>();
}
[TestMethod]
public void TestSha256()
{
byte[] value = Encoding.ASCII.GetBytes("hello world");
byte[] result = value.Sha256(0, value.Length);
string resultStr = result.ToHexString();
resultStr.Should().Be("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9");
}
[TestMethod]
public void TestRIPEMD160()
{
ReadOnlySpan<byte> value = Encoding.ASCII.GetBytes("hello world");
byte[] result = value.RIPEMD160();
string resultStr = result.ToHexString();
resultStr.Should().Be("98c615784ccb5fe5936fbc0cbe9dfdb408d92f0f");
}
[TestMethod]
public void TestAESEncryptAndDecrypt()
{
NEP6Wallet wallet = new NEP6Wallet("", ProtocolSettings.Default);
wallet.Unlock("1");
wallet.CreateAccount();
WalletAccount account = wallet.GetAccounts().ToArray()[0];
KeyPair key = account.GetKey();
Random random = new Random();
byte[] nonce = new byte[12];
random.NextBytes(nonce);
var cypher = Neo.Cryptography.Helper.AES256Encrypt(Encoding.UTF8.GetBytes("hello world"), key.PrivateKey, nonce);
var m = Neo.Cryptography.Helper.AES256Decrypt(cypher, key.PrivateKey);
var message2 = Encoding.UTF8.GetString(m);
Assert.AreEqual("hello world", message2);
}
[TestMethod]
public void TestEcdhEncryptAndDecrypt()
{
NEP6Wallet wallet = new NEP6Wallet("", ProtocolSettings.Default);
wallet.Unlock("1");
wallet.CreateAccount();
wallet.CreateAccount();
WalletAccount account1 = wallet.GetAccounts().ToArray()[0];
KeyPair key1 = account1.GetKey();
WalletAccount account2 = wallet.GetAccounts().ToArray()[1];
KeyPair key2 = account2.GetKey();
Console.WriteLine($"Account:{1},privatekey:{key1.PrivateKey.ToHexString()},publicKey:{key1.PublicKey.ToArray().ToHexString()}");
Console.WriteLine($"Account:{2},privatekey:{key2.PrivateKey.ToHexString()},publicKey:{key2.PublicKey.ToArray().ToHexString()}");
var message = Encoding.ASCII.GetBytes("hello world");
var cypher1 = Neo.Cryptography.Helper.EcdhEncrypt(message, key1, key2.PublicKey);
var cypher2 = Neo.Cryptography.Helper.EcdhDecrypt(cypher1, key2, key1.PublicKey);
Assert.AreEqual("hello world", Encoding.ASCII.GetString(cypher2));
}
[TestMethod]
public void TestTest()
{
int m = 7, n = 10;
uint nTweak = 123456;
BloomFilter filter = new(m, n, nTweak);
Transaction tx = new()
{
Script = TestUtils.GetByteArray(32, 0x42),
SystemFee = 4200000000,
Signers = new Signer[] { new Signer() { Account = (Array.Empty<byte>()).ToScriptHash() } },
Attributes = Array.Empty<TransactionAttribute>(),
Witnesses = new[]
{
new Witness
{
InvocationScript = Array.Empty<byte>(),
VerificationScript = Array.Empty<byte>()
}
}
};
filter.Test(tx).Should().BeFalse();
filter.Add(tx.Witnesses[0].ScriptHash.ToArray());
filter.Test(tx).Should().BeTrue();
filter.Add(tx.Hash.ToArray());
filter.Test(tx).Should().BeTrue();
}
[TestMethod]
public void TestStringToAesKey()
{
string password = "hello world";
string string1 = "bc62d4b80d9e36da29c16c5d4d9f11731f36052c72401a76c23c0fb5a9b74423";
byte[] byteArray = string1.HexToBytes();
password.ToAesKey().Should().Equal(byteArray);
}
[TestMethod]
public void TestSecureStringToAesKey()
{
var password = new SecureString();
password.AppendChar('h');
password.AppendChar('e');
password.AppendChar('l');
password.AppendChar('l');
password.AppendChar('o');
password.AppendChar(' ');
password.AppendChar('w');
password.AppendChar('o');
password.AppendChar('r');
password.AppendChar('l');
password.AppendChar('d');
string string1 = "bc62d4b80d9e36da29c16c5d4d9f11731f36052c72401a76c23c0fb5a9b74423";
byte[] byteArray = string1.HexToBytes();
password.ToAesKey().Should().Equal(byteArray);
}
[TestMethod]
public void TestToArray()
{
var password = new SecureString();
password.AppendChar('h');
password.AppendChar('e');
password.AppendChar('l');
password.AppendChar('l');
password.AppendChar('o');
password.AppendChar(' ');
password.AppendChar('w');
password.AppendChar('o');
password.AppendChar('r');
password.AppendChar('l');
password.AppendChar('d');
byte[] byteArray = { 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64 };
password.ToArray().Should().Equal(byteArray);
SecureString nullString = null;
Action action = () => nullString.ToArray();
action.Should().Throw<NullReferenceException>();
var zeroString = new SecureString();
var result = zeroString.ToArray();
byteArray = Array.Empty<byte>();
result.Should().Equal(byteArray);
}
}
}