|
10 | 10 |
|
11 | 11 | using Neo.IO;
|
12 | 12 | using Neo.Network.P2P.Payloads;
|
| 13 | +using Neo.Wallets; |
13 | 14 | using System;
|
14 | 15 | using System.Buffers.Binary;
|
15 | 16 | using System.Collections.Generic;
|
|
18 | 19 | using System.Security;
|
19 | 20 | using System.Security.Cryptography;
|
20 | 21 | using System.Text;
|
| 22 | +using static Neo.Helper; |
| 23 | +using ECPoint = Neo.Cryptography.ECC.ECPoint; |
21 | 24 |
|
22 | 25 | namespace Neo.Cryptography
|
23 | 26 | {
|
@@ -119,6 +122,54 @@ public static byte[] Sha256(this Span<byte> value)
|
119 | 122 | return Sha256((ReadOnlySpan<byte>)value);
|
120 | 123 | }
|
121 | 124 |
|
| 125 | + public static byte[] AES256Encrypt(this byte[] plainData, byte[] key, byte[] nonce, byte[] associatedData = null) |
| 126 | + { |
| 127 | + if (nonce.Length != 12) throw new ArgumentOutOfRangeException(nameof(nonce)); |
| 128 | + var cipherBytes = new byte[plainData.Length]; |
| 129 | + var tag = new byte[16]; |
| 130 | + using var cipher = new AesGcm(key); |
| 131 | + cipher.Encrypt(nonce, plainData, cipherBytes, tag, associatedData); |
| 132 | + return Concat(nonce, cipherBytes, tag); |
| 133 | + } |
| 134 | + |
| 135 | + public static byte[] AES256Decrypt(this byte[] encryptedData, byte[] key, byte[] associatedData = null) |
| 136 | + { |
| 137 | + ReadOnlySpan<byte> encrypted = encryptedData; |
| 138 | + var nonce = encrypted[..12]; |
| 139 | + var cipherBytes = encrypted[12..^16]; |
| 140 | + var tag = encrypted[^16..]; |
| 141 | + var decryptedData = new byte[cipherBytes.Length]; |
| 142 | + using var cipher = new AesGcm(key); |
| 143 | + cipher.Decrypt(nonce, cipherBytes, tag, decryptedData, associatedData); |
| 144 | + return decryptedData; |
| 145 | + } |
| 146 | + |
| 147 | + public static byte[] ECDHDeriveKey(KeyPair local, ECPoint remote) |
| 148 | + { |
| 149 | + ReadOnlySpan<byte> pubkey_local = local.PublicKey.EncodePoint(false); |
| 150 | + ReadOnlySpan<byte> pubkey_remote = remote.EncodePoint(false); |
| 151 | + using ECDiffieHellman ecdh1 = ECDiffieHellman.Create(new ECParameters |
| 152 | + { |
| 153 | + Curve = ECCurve.NamedCurves.nistP256, |
| 154 | + D = local.PrivateKey, |
| 155 | + Q = new System.Security.Cryptography.ECPoint |
| 156 | + { |
| 157 | + X = pubkey_local[1..][..32].ToArray(), |
| 158 | + Y = pubkey_local[1..][32..].ToArray() |
| 159 | + } |
| 160 | + }); |
| 161 | + using ECDiffieHellman ecdh2 = ECDiffieHellman.Create(new ECParameters |
| 162 | + { |
| 163 | + Curve = ECCurve.NamedCurves.nistP256, |
| 164 | + Q = new System.Security.Cryptography.ECPoint |
| 165 | + { |
| 166 | + X = pubkey_remote[1..][..32].ToArray(), |
| 167 | + Y = pubkey_remote[1..][32..].ToArray() |
| 168 | + } |
| 169 | + }); |
| 170 | + return ecdh1.DeriveKeyMaterial(ecdh2.PublicKey).Sha256();//z = r * P = r* k * G |
| 171 | + } |
| 172 | + |
122 | 173 | internal static bool Test(this BloomFilter filter, Transaction tx)
|
123 | 174 | {
|
124 | 175 | if (filter.Check(tx.Hash.ToArray())) return true;
|
|
0 commit comments