public void ShouldRecoverGethPrefix() { //signed message using geth 1.5^ var signature = "0x0976a177078198a261faf206287b8bb93ebb233347ab09a57c8691733f5772f67f398084b30fc6379ffee2cc72d510fd0f8a7ac2ee0162b95dc5d61146b40ffa1c"; var text = "test"; var hasher = new Sha3Keccack(); var hash = hasher.CalculateHash(text); var byteList = new List <byte>(); var bytePrefix = "0x19".HexToByteArray(); var textBytePrefix = Encoding.UTF8.GetBytes("Ethereum Signed Message:\n" + hash.HexToByteArray().Length); var bytesMessage = hash.HexToByteArray(); byteList.AddRange(bytePrefix); byteList.AddRange(textBytePrefix); byteList.AddRange(bytesMessage); var hashPrefix2 = hasher.CalculateHash(byteList.ToArray()).ToHex(); var signer = new MessageSigner(); var account = signer.EcRecover(hashPrefix2.HexToByteArray(), signature); Assert.Equal("0x12890d2cce102216644c59dae5baed380d84830c", account.EnsureHexPrefix().ToLower()); signature = signer.Sign(hashPrefix2.HexToByteArray(), "0xb5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7"); account = signer.EcRecover(hashPrefix2.HexToByteArray(), signature); Assert.Equal("0x12890d2cce102216644c59dae5baed380d84830c".ToLower(), account.EnsureHexPrefix().ToLower()); }
public static bool ExistsOnDiskWithSameContent(this GeneratedFile generatedFile) { if (!File.Exists(generatedFile.GetFullPath())) { return(false); } var newHash = Sha3.CalculateHash(generatedFile.GeneratedCode); var existingHash = Sha3.CalculateHash(File.ReadAllText(generatedFile.GetFullPath())); return(newHash == existingHash); }
/// <summary> /// Hash structured data /// </summary> /// <typeparam name="T">Structured data datatype</typeparam> /// <param name="structure">Structured data to hash</param> /// <param name="domain">EIP-712 domain</param> /// <returns>Keccak256 of encoded data</returns> /// <exception cref="ArgumentNullException"><paramref name="domain"/> or <paramref name="structure"/> /// is equal to <c>null</c></exception> public static byte[] Hash <T>(T structure, EIP712Domain domain) where T : class { if (structure == null) { throw new ArgumentNullException(nameof(structure)); } if (domain == null) { throw new ArgumentNullException(nameof(domain)); } return(_keccak.CalculateHash(Encode(structure, domain))); }
public ContractCallParser(IEnumerable <Tuple <string, int> > abis) { var hash = new Sha3Keccack(); foreach (var tuple in abis) { var key = BitConverter.ToString(hash.CalculateHash(Encoding.UTF8.GetBytes(tuple.Item1)).Take(4).ToArray()).Replace("-", string.Empty).ToLower(); _map[key] = tuple.Item2; } }
private string CreateHash(TEntity entity) { var values = entity.GetType().GetProperties() .Where(p => p.Name != nameof(entity.RowIndex)) .Where(p => p.Name != nameof(entity.RowCreated)) .Where(p => p.Name != nameof(entity.RowUpdated)) .Select(p => p.GetValue(entity)) .Where(v => v != null) .Select(v => v.ToString()); return(_hasher.CalculateHash(string.Join("~", values))); }
public void test5() { var signature = "0x7c87377d7a148ee69ec5ed9be2c87033639b6970acf9a8e165e81cf3dce4516d2bdcc6957eb435447ef9eaa34f541a81f999f0b967bfbfcca1850f58f4cdf5fe1c"; var text = "0x492d0fd814940d1375225a7e10905585b72b0a8c;-7440852294285764000;636706243532255384"; var hasher = new Sha3Keccack(); var hash = hasher.CalculateHash(text); var signer = new EthereumMessageSigner(); var account = signer.EncodeUTF8AndEcRecover(text, signature); Assert.Equal("0x492d0fd814940d1375225a7e10905585b72b0a8c".ToLower(), account.EnsureHexPrefix().ToLower()); }
private string GenerateChannelName() { byte[] randomBytes = new byte[32]; Random rng = new Random(); rng.NextBytes(randomBytes); Sha3Keccack hasher = new Sha3Keccack(); string key = Encoding.UTF8.GetString(randomBytes, 0, 32); string hash = hasher.CalculateHash(key); return(hash.Substring(0, 7)); }
public string RecoveryBouncy() { EthECKey.SignRecoverable = false; var signature = "0x0976a177078198a261faf206287b8bb93ebb233347ab09a57c8691733f5772f67f398084b30fc6379ffee2cc72d510fd0f8a7ac2ee0162b95dc5d61146b40ffa1c"; var text = "test"; var hasher = new Sha3Keccack(); var hash = hasher.CalculateHash(text); var signer = new EthereumMessageSigner(); var account = signer.EcRecover(hash.HexToByteArray(), signature); return(account); }
private string CreateHash(TEntity entity) { var values = _propertiesForHash .Select(p => p.GetValue(entity)) .Where(v => v != null) .Select(v => v.ToString()) .Where(s => s.Length > 0); var key = string.Join("~", values); var hash = _hasher.CalculateHash(key); return(hash); }
public string GetEnsNameHash(string name) { var node = "0x0000000000000000000000000000000000000000000000000000000000000000"; var kecckak = new Sha3Keccack(); if (!string.IsNullOrEmpty(name)) { var labels = name.Split('.'); for (var i = labels.Length - 1; i >= 0; i--) { var byteInput = (node + GetEnsLabelHash(labels[i])).HexToByteArray(); node = kecckak.CalculateHash(byteInput).ToHex(); } } return(node.EnsureHexPrefix()); }
public void ShouldRecover() { var signature = "0x0976a177078198a261faf206287b8bb93ebb233347ab09a57c8691733f5772f67f398084b30fc6379ffee2cc72d510fd0f8a7ac2ee0162b95dc5d61146b40ffa1c"; var text = "test"; var hasher = new Sha3Keccack(); var hash = hasher.CalculateHash(text); var signer = new EthereumMessageSigner(); var account = signer.EcRecover(hash.HexToByteArray(), signature); Assert.Equal("0x12890d2cce102216644c59dae5baed380d84830c", account.EnsureHexPrefix().ToLower()); signature = signer.Sign(hash.HexToByteArray(), "0xb5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7"); account = signer.EcRecover(hash.HexToByteArray(), signature); Assert.Equal("0x12890d2cce102216644c59dae5baed380d84830c".ToLower(), account.EnsureHexPrefix().ToLower()); }
internal string GetStartLowerCase(EthECKey key, int length) { byte[] hash = _sha3Keccack.CalculateHash(key.GetPubKeyNoPrefix()); byte[] numArray = new byte[hash.Length - 12]; Array.Copy(hash, 12, numArray, 0, hash.Length - 12); string address = "0x"; for (int i = 0; i < numArray.Length; i++) { address += numArray[i].ToString("x2"); if (address.Length >= length) { return(address); } } return(address); }
public void test4() { var signature = "0xdb46c6be4b1ee0ef670625630d38899213ea5d63d749954d82a4651b81f7d44f6d16e2e4903038c022b71d11baa26b08142c6b9f5db106d7b96b57d1836ce10e1b"; var text = "test"; var hasher = new Sha3Keccack(); var hash = hasher.CalculateHash(text); var signer = new EthereumMessageSigner(); var account = signer.EncodeUTF8AndEcRecover(text, signature); Assert.Equal("0xe77992311815D6961cBB231CFd2009990203385F".ToLower(), account.EnsureHexPrefix().ToLower()); signature = signer.Sign(hash.HexToByteArray(), "0xd3a7d42d881a9b59ccefcac0f5bcc69f85e68fdf0bfb6fcbbe42373320de420f"); account = signer.EcRecover(hash.HexToByteArray(), signature); Assert.Equal("0x12890d2cce102216644c59dae5baed380d84830c".ToLower(), account.EnsureHexPrefix().ToLower()); }
public string GetNameHash(string name) { #if !DOTNET35 && !NETSTANDARD1_1 var node = "0x0000000000000000000000000000000000000000000000000000000000000000"; var kecckak = new Sha3Keccack(); if (!string.IsNullOrEmpty(name)) { name = Normalise(name); var labels = name.Split('.'); for (var i = labels.Length - 1; i >= 0; i--) { var byteInput = (node + GetLabelHash(labels[i])).HexToByteArray(); node = kecckak.CalculateHash(byteInput).ToHex(); } } return(node.EnsureHexPrefix()); #else throw new Exception("GetNameHash unsupported for the current .net version"); #endif }
public string sign(byte[] privateKey, dynamic chainId) { this.storageLimit = BitConverter.GetBytes(100000).Clear(); this.chianId = BitConverter.GetBytes((int)chainId).Clear(); //this.Gas = BitConverter.GetBytes(2000000).Clear(); List <byte[]> raw = new List <byte[]> { this._nonce, this.GasPrice, this.Gas, this.to, this.Value, this.storageLimit, this._epochHeight, this.chianId, this.Data }; var x1 = rlpEncode(raw).ToHex(); var x2 = sha3Keccack.CalculateHash(rlpEncode(raw));//sha3 var k = new CfxECKey("0x" + privateKey.ToHex()); var x3 = k.SignAndCalculateV(x2); //x3.V = new byte[] { 0x01 }; List <object> rawWithRSV = new List <object> { raw, x3.V, x3.R, x3.S }; var x4 = rlpEncode(rawWithRSV).ToHex(); return("0x" + x4); }
public string GenerateSha3Signature(string name, Parameter[] parameters) { var signature = GenerateSignature(name, parameters); return(sha3Keccack.CalculateHash(signature)); }
public string Sha3(string value) { return(sha3Keccack.CalculateHash(value)); }
public string GetLabelHash(string label) { var kecckak = new Sha3Keccack(); return(kecckak.CalculateHash(label)); }