private static bool ValidateChecksum( string addressString) { addressString = addressString.Remove(0, 2); var addressBytes = Encoding.UTF8.GetBytes(addressString.ToLowerInvariant()); var caseMapBytes = Keccak256.Sum(addressBytes); for (var i = 0; i < 40; i++) { var addressChar = addressString[i]; if (!char.IsLetter(addressChar)) { continue; } var leftShift = i % 2 == 0 ? 7 : 3; var shouldBeUpper = (caseMapBytes[i / 2] & (1 << leftShift)) != 0; var shouldBeLower = !shouldBeUpper; if (shouldBeUpper && char.IsLower(addressChar) || shouldBeLower && char.IsUpper(addressChar)) { return(false); } } return(true); }
public static string AddChecksum( string addressString) { ValidateFormatAndThrowIfInvalid(addressString); addressString = addressString.Remove(0, 2).ToLowerInvariant(); var addressBytes = Encoding.UTF8.GetBytes(addressString); var caseMapBytes = Keccak256.Sum(addressBytes); var addressBuilder = new StringBuilder("0x"); for (var i = 0; i < 40; i++) { var addressChar = addressString[i]; if (char.IsLetter(addressChar)) { var leftShift = i % 2 == 0 ? 7 : 3; var shouldBeUpper = (caseMapBytes[i / 2] & (1 << leftShift)) != 0; if (shouldBeUpper) { addressChar = char.ToUpper(addressChar); } } addressBuilder.Append(addressChar); } return(addressBuilder.ToString()); }
public static byte[] GetCredentials() { // unique credential should be at least these symbols; comp-name, sid, app-code // obscure the code and shift in file; make it hard to find string user = GetComputerName() + GetUserSid() + "x0024b88t72:im@vtc#s1d1"; using (Keccak256 digest = new Keccak256()) return(digest.ComputeHash(Encoding.UTF8.GetBytes(user))); }
private static string GetTransactionHash( string signedTxData) { var txDataBytes = signedTxData.HexToByteArray(); return(Keccak256 .Sum(txDataBytes) .ToHex(true)); }
private void OnGenerateButtonClick(object sender, EventArgs e) { byte[] data = Encoding.UTF8.GetBytes(txtPassphrase.Text); using (Keccak256 digest = new Keccak256()) Passphrase = digest.ComputeHash(data); this.Hide(); }
/// <summary> /// Computes a Lyra2RE hash. /// </summary> /// <param name="input">The input buffer.</param> /// <returns>The computed hash.</returns> public static byte[] ComputeHash(byte[] input) { var output = new Blake256().ComputeBytes(input).GetBytes(); output = new Keccak256().ComputeBytes(output).GetBytes(); output = new Lyra2(Lyra2Version.v1).ComputeBytes(32, output, output, 1, 8, 8); output = new Skein256().ComputeBytes(output).GetBytes(); output = new Groestl256().ComputeBytes(output).GetBytes(); return(output); }
public byte[] CalculateHash(byte[] value) { var digest = new Keccak256(); var output = new byte[digest.BlockSize]; digest.TransformBytes(value, 0, value.Length); var result = digest.TransformFinal(); output = result.GetBytes(); return(output); }
/// <summary> /// Computes a Lyra2REv2 hash. /// </summary> /// <param name="input">The input buffer.</param> /// <returns>The computed hash.</returns> public static byte[] ComputeHash(byte[] input) { var output = new Blake256().ComputeBytes(input).GetBytes(); output = new Keccak256().ComputeBytes(output).GetBytes(); output = new CubeHash256().ComputeBytes(output).GetBytes(); output = new Lyra2(Lyra2Version.v2).ComputeBytes(32, output, output, 1, 4, 4); output = new Skein256().ComputeBytes(output).GetBytes(); output = new CubeHash256().ComputeBytes(output).GetBytes(); output = new BlueMidnightWish256().ComputeBytes(output).GetBytes(); return(output); }
public static Address Generate(string _privateKey = "") { Address _address = new Address(); _address.Private = _privateKey == "" ? RandomPlus.RandomHex(64) : _privateKey; _address.Public = HexPlus.ByteArrayToHexString(Secp256k1.PrivateKeyToPublicKey(_address.Private)); _address.Public = _address.Public.Substring(2); Keccak256 _keccakHasher = new Keccak256(); string _hexAddress = _keccakHasher.ComputeHashByHex(_address.Public); _address.Text = "0x" + _hexAddress.Substring(_hexAddress.Length - 40); return(_address); }
public static byte[] GetOriginId() { string user = GetUserName() + GetUserSid(); byte[] hash; using (Keccak256 digest = new Keccak256()) hash = digest.ComputeHash(Encoding.UTF8.GetBytes(user)); byte[] id = new byte[16]; for (int i = 0, j = 16; i < 16; i++, j++) { id[i] = (byte)(hash[i] ^ hash[j]); } return(id); }
public static byte[] GetDomainId() { string domain = GetDomainName(); if (string.IsNullOrEmpty(domain)) { domain = GetComputerName(); } int blockSize = domain.Length < 32 ? 32 : domain.Length; byte[] data = new byte[blockSize]; byte[] name = Encoding.UTF8.GetBytes(domain); Buffer.BlockCopy(name, 0, data, 0, name.Length); using (Keccak256 digest = new Keccak256()) return(digest.ComputeHash(Encoding.UTF8.GetBytes(domain))); }
public string ToSignedHex(string _private) { byte[] _basicRaw = RLP.EncodeList(new byte[][] { RLP.EncodeUInt(this.Nonce), RLP.EncodeBigInteger(this.GasPrice.ToGWei()), RLP.EncodeUInt(this.GasLimit), RLP.EncodeHex(this.Address), RLP.EncodeBigInteger(this.Value.Integer), RLP.EncodeString(this.DataHex), RLP.EncodeInt((int)this.ChainId), RLP.EncodeString(""), RLP.EncodeString("") }); byte[] _basicHashedRaw = new Keccak256().Compute(_basicRaw); BigInteger _limit = BigInteger.Pow(BigInteger.Parse("2"), 256), _r = BigInteger.Zero, _e = BigInteger.Zero, _s = BigInteger.Zero, _k = BigInteger.Zero, _recid = BigInteger.Zero; while (true) { _k = BigInteger.Zero; if (_k == BigInteger.Zero) { byte[] kBytes = new byte[33]; rngCsp.GetBytes(kBytes); kBytes[32] = 0; _k = new BigInteger(kBytes); } if (_k.IsZero || _k >= Secp256k1.N) { continue; } var _gk = Secp256k1.G.Multiply(_k); _r = _gk.X % Secp256k1.N; _recid = _gk.Y & 1; if (_r == BigInteger.Zero) { throw new Exception("Sign failed because R is Zero."); } if (_r >= _limit || _r.Sign == 0) { Thread.Sleep(100); continue; } _e = Lion.BigNumberPlus.HexToBigInt(BitConverter.ToString(_basicHashedRaw).Replace("-", "")); _s = ((_e + (_r * Lion.BigNumberPlus.HexToBigInt(_private))) * BigInteger.ModPow(_k, Secp256k1.N - 2, Secp256k1.N)) % Secp256k1.N; if (_s == BigInteger.Zero) { throw new Exception("Sign failed because S is Zero."); } if (_s > Secp256k1.HalfN) { _recid = _recid ^ 1; } if (_s.CompareTo(Secp256k1.HalfN) > 0) { _s = Secp256k1.N - _s; } if (_s >= _limit || _s.Sign == 0 || _r.ToString("X").StartsWith("0") || _s.ToString("X").StartsWith("0")) { Thread.Sleep(100); continue; } break; } BigInteger _v = BigInteger.Parse(((int)this.ChainId).ToString()) * 2 + _recid + 35; byte[] _signed = RLP.EncodeList(new byte[][] { RLP.EncodeUInt(this.Nonce), RLP.EncodeBigInteger(this.GasPrice.ToGWei()), RLP.EncodeUInt(this.GasLimit), RLP.EncodeHex(this.Address), RLP.EncodeBigInteger(this.Value.Integer), RLP.EncodeString(this.DataHex), RLP.EncodeBigInteger(_v), RLP.EncodeBytes(HexPlus.HexStringToByteArray(_r.ToString("X"))), RLP.EncodeBytes(HexPlus.HexStringToByteArray(_s.ToString("X"))) }); return(HexPlus.ByteArrayToHexString(_signed).ToLower()); }
public byte[] Keccak(byte[] input) { Keccak256.Initialize(); Keccak256.TransformBytes(input.ToArray(), 0, input.Count()); return(Keccak256.TransformFinal().GetBytes()); }
public static string BuildRawTransaction(BigInteger _chainId, BigInteger _nonce, BigInteger _gasPrice, BigInteger _gasLimit, string _fromPrivateKey, string _addressTo, BigInteger _amount, string _data = "") { var _amountDex = _amount.ToString("X");//ToETHValue(_amount.ToString()); var _basicRaw = Lion.Encrypt.RLP.EncodeList(new byte[][] { ToBytesForRLPEncoding(_nonce), ToBytesForRLPEncoding(_gasPrice), ToBytesForRLPEncoding(_gasLimit), HexToByteArray(_addressTo), ToBytesForRLPEncoding(BigInteger.Parse(_amountDex, System.Globalization.NumberStyles.HexNumber)), HexToByteArray(_data), RLP.EncodeElement(_chainId.ToByteArray()), HexToByteArray(""), HexToByteArray("") }); var _basicRawSHA = new Keccak256().Compute(_basicRaw); var _r = BigInteger.Zero; var _e = BigInteger.Zero; var _s = BigInteger.Zero; var _k = BigInteger.Zero; var _recid = BigInteger.Zero; while (true) { _k = new Random(Lion.RandomPlus.RandomSeed).Next(1, int.MaxValue); var _Gk = Secp256k1.G.Multiply(_k); var _y = _Gk.Y & 1; _r = _Gk.X; _r = _r % Secp256k1.N; if (_r >= KeyLimit || _r.Sign == 0) { Thread.Sleep(500); continue; } _e = BigInteger.Parse($"0{BitConverter.ToString(_basicRawSHA).Replace("-", "")}", System.Globalization.NumberStyles.HexNumber); var _d = BigInteger.Parse($"0{_fromPrivateKey}", System.Globalization.NumberStyles.HexNumber); _s = _r * _d; _s = _s + _e; _s = _s * _k.ModInverse(Secp256k1.N); _s = _s % Secp256k1.N; if (_s > Secp256k1.HalfN) { _recid = _y; } if (_s.CompareTo(Secp256k1.HalfN) > 0) { _s = Secp256k1.N - _s; } if (_s >= KeyLimit || _s.Sign == 0) { Thread.Sleep(500); continue; } break; } if (_k == BigInteger.Zero || _r == BigInteger.Zero || _e == BigInteger.Zero || _s == BigInteger.Zero) { throw new Exception("Transaction sign error"); } BigInteger _v = _chainId * 2 + _recid + 35; return(BitConverter.ToString(Lion.Encrypt.RLP.EncodeList(new byte[][] { ToBytesForRLPEncoding(_nonce), ToBytesForRLPEncoding(_gasPrice), ToBytesForRLPEncoding(_gasLimit), HexToByteArray(_addressTo), ToBytesForRLPEncoding(BigInteger.Parse(_amountDex, System.Globalization.NumberStyles.HexNumber)), HexToByteArray(_data), ToBytesForRLPEncoding(_v), RLP.EncodeElement(Lion.HexPlus.HexStringToByteArray(_r.ToString("X").TrimStart('0'))), RLP.EncodeElement(Lion.HexPlus.HexStringToByteArray(_s.ToString("X").TrimStart('0'))) })).Replace("-", "")); }