Skip to content

Commit

Permalink
Change Scrypt from source to nuget (neo-project#2025)
Browse files Browse the repository at this point in the history
* Change from source to nuget

* Use SCrypt.Generate directly

* Fix UT

Co-authored-by: Erik Zhang <erik@neo.org>
  • Loading branch information
2 people authored and cloud8little committed Jan 24, 2021
1 parent 398a5be commit 3e751ac
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 351 deletions.
323 changes: 0 additions & 323 deletions src/neo/Cryptography/SCrypt.cs

This file was deleted.

3 changes: 2 additions & 1 deletion src/neo/Wallets/KeyPair.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Neo.Cryptography;
using Neo.SmartContract;
using Org.BouncyCastle.Crypto.Generators;
using System;
using System.Text;
using static Neo.Wallets.Helper;
Expand Down Expand Up @@ -56,7 +57,7 @@ public string Export(string passphrase, int N = 16384, int r = 8, int p = 8)
UInt160 script_hash = Contract.CreateSignatureRedeemScript(PublicKey).ToScriptHash();
string address = script_hash.ToAddress();
byte[] addresshash = Encoding.ASCII.GetBytes(address).Sha256().Sha256()[..4];
byte[] derivedkey = SCrypt.DeriveKey(Encoding.UTF8.GetBytes(passphrase), addresshash, N, r, p, 64);
byte[] derivedkey = SCrypt.Generate(Encoding.UTF8.GetBytes(passphrase), addresshash, N, r, p, 64);
byte[] derivedhalf1 = derivedkey[..32];
byte[] derivedhalf2 = derivedkey[32..];
byte[] encryptedkey = XOR(PrivateKey, derivedhalf1).AES256Encrypt(derivedhalf2);
Expand Down
3 changes: 2 additions & 1 deletion src/neo/Wallets/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Neo.SmartContract.Manifest;
using Neo.SmartContract.Native;
using Neo.VM;
using Org.BouncyCastle.Crypto.Generators;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -162,7 +163,7 @@ public static byte[] GetPrivateKeyFromNEP2(string nep2, string passphrase, int N
byte[] addresshash = new byte[4];
Buffer.BlockCopy(data, 3, addresshash, 0, 4);
byte[] datapassphrase = Encoding.UTF8.GetBytes(passphrase);
byte[] derivedkey = SCrypt.DeriveKey(datapassphrase, addresshash, N, r, p, 64);
byte[] derivedkey = SCrypt.Generate(datapassphrase, addresshash, N, r, p, 64);
Array.Clear(datapassphrase, 0, datapassphrase.Length);
byte[] derivedhalf1 = derivedkey[..32];
byte[] derivedhalf2 = derivedkey[32..];
Expand Down
1 change: 1 addition & 0 deletions src/neo/neo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

<ItemGroup>
<PackageReference Include="Akka" Version="1.4.5" />
<PackageReference Include="BouncyCastle.NetCore" Version="1.8.8" />
<PackageReference Include="K4os.Compression.LZ4" Version="1.1.11" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.WebSockets" Version="2.2.1" />
Expand Down
4 changes: 2 additions & 2 deletions tests/neo.UnitTests/Cryptography/UT_SCrypt.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Cryptography;
using Org.BouncyCastle.Crypto.Generators;

namespace Neo.UnitTests.Cryptography
{
Expand All @@ -11,7 +11,7 @@ public void DeriveKeyTest()
{
int N = 32, r = 2, p = 2;

var derivedkey = SCrypt.DeriveKey(new byte[] { 0x01, 0x02, 0x03 }, new byte[] { 0x04, 0x05, 0x06 }, N, r, p, 64).ToHexString();
var derivedkey = SCrypt.Generate(new byte[] { 0x01, 0x02, 0x03 }, new byte[] { 0x04, 0x05, 0x06 }, N, r, p, 64).ToHexString();
Assert.AreEqual("b6274d3a81892c24335ab46a08ec16d040ac00c5943b212099a44b76a9b8102631ab988fa07fb35357cee7b0e3910098c0774c0e97399997676d890b2bf2bb25", derivedkey);
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/neo.UnitTests/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ public static NEP6Wallet GenerateTestWallet()
JObject wallet = new JObject();
wallet["name"] = "noname";
wallet["version"] = new Version("3.0").ToString();
wallet["scrypt"] = new ScryptParameters(0, 0, 0).ToJson();
wallet["scrypt"] = new ScryptParameters(2, 1, 1).ToJson();
wallet["accounts"] = new JArray();
wallet["extra"] = null;
wallet.ToString().Should().Be("{\"name\":\"noname\",\"version\":\"3.0\",\"scrypt\":{\"n\":0,\"r\":0,\"p\":0},\"accounts\":[],\"extra\":null}");
wallet.ToString().Should().Be("{\"name\":\"noname\",\"version\":\"3.0\",\"scrypt\":{\"n\":2,\"r\":1,\"p\":1},\"accounts\":[],\"extra\":null}");
return new NEP6Wallet(wallet);
}

Expand Down
Loading

0 comments on commit 3e751ac

Please sign in to comment.