Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean memory #868

Merged
merged 5 commits into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions neo/Cryptography/Base58.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static byte[] Decode(string input)
}
byte[] tmp = new byte[bytes.Length - (stripSignByte ? 1 : 0) + leadingZeros];
Array.Copy(bytes, stripSignByte ? 1 : 0, tmp, leadingZeros, tmp.Length - leadingZeros);
Array.Clear(bytes, 0, bytes.Length);
return tmp;
}

Expand Down
8 changes: 6 additions & 2 deletions neo/Cryptography/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ public static byte[] Base58CheckDecode(this string input)
byte[] checksum = buffer.Sha256(0, buffer.Length - 4).Sha256();
if (!buffer.Skip(buffer.Length - 4).SequenceEqual(checksum.Take(4)))
throw new FormatException();
return buffer.Take(buffer.Length - 4).ToArray();
var ret = buffer.Take(buffer.Length - 4).ToArray();
Array.Clear(buffer, 0, buffer.Length);
return ret;
}

public static string Base58CheckEncode(this byte[] data)
Expand All @@ -88,7 +90,9 @@ public static string Base58CheckEncode(this byte[] data)
byte[] buffer = new byte[data.Length + 4];
Buffer.BlockCopy(data, 0, buffer, 0, data.Length);
Buffer.BlockCopy(checksum, 0, buffer, data.Length, 4);
return Base58.Encode(buffer);
var ret = Base58.Encode(buffer);
Array.Clear(buffer, 0, buffer.Length);
return ret;
}

public static byte[] RIPEMD160(this IEnumerable<byte> value)
Expand Down
8 changes: 7 additions & 1 deletion neo/Wallets/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,18 @@ public static byte[] GetPrivateKeyFromNEP2(string nep2, string passphrase, int N
throw new FormatException();
byte[] addresshash = new byte[4];
Buffer.BlockCopy(data, 3, addresshash, 0, 4);
byte[] derivedkey = SCrypt.DeriveKey(Encoding.UTF8.GetBytes(passphrase), addresshash, N, r, p, 64);
byte[] datapassphrase = Encoding.UTF8.GetBytes(passphrase);
byte[] derivedkey = SCrypt.DeriveKey(datapassphrase, addresshash, N, r, p, 64);
Array.Clear(datapassphrase, 0, datapassphrase.Length);
byte[] derivedhalf1 = derivedkey.Take(32).ToArray();
byte[] derivedhalf2 = derivedkey.Skip(32).ToArray();
Array.Clear(derivedkey, 0, derivedkey.Length);
byte[] encryptedkey = new byte[32];
Buffer.BlockCopy(data, 7, encryptedkey, 0, 32);
Array.Clear(data, 0, data.Length);
byte[] prikey = XOR(encryptedkey.AES256Decrypt(derivedhalf2), derivedhalf1);
Array.Clear(derivedhalf1, 0, derivedhalf1.Length);
Array.Clear(derivedhalf2, 0, derivedhalf2.Length);
ECPoint pubkey = Cryptography.ECC.ECCurve.Secp256r1.G * prikey;
UInt160 script_hash = Contract.CreateSignatureRedeemScript(pubkey).ToScriptHash();
string address = script_hash.ToAddress();
Expand Down