public static byte[] Hmac(this byte[] data, byte[] key) { var blockSize = 136; var ipad = new byte[blockSize]; var opad = new byte[blockSize]; if (key.Length > blockSize) { key = new SHA3_256().ComputeVariable(key); } else if (key.Length < blockSize) { var temp = new byte[blockSize]; Array.Copy(key, temp, key.Length); Array.Clear(temp, 128, blockSize - 128); key = temp; } for (var i = 0; i < blockSize; i++) { ipad[i] = i < key.Length ? (byte)(key[i] ^ 0x36) : (byte)0x36; opad[i] = i < key.Length ? (byte)(key[i] ^ 0x5C) : (byte)0x5C; } var hash1 = new SHA3_256().ComputeVariable(ipad.Concat(data).ToArray()); var hash2 = new SHA3_256().ComputeVariable(opad.Concat(hash1).ToArray()); return(hash2); }
public void Test_04_Performance() { byte[] Data = new byte[80 * 1024 * 1024]; SHA3_256 H = new SHA3_256(); H.ComputeVariable(Data); }
public void Test_02_1600_bits() { SHA3_256 H = new SHA3_256(); int i = 0; H.NewState += (sender, e) => { string Expected = States1600Bits[i++].Replace(" ", string.Empty); string Actual = Hashes.BinaryToString(H.GetState()).ToUpper(); Assert.AreEqual(Expected, Actual); }; byte[] Input = new byte[200]; int j; for (j = 0; j < 200; j++) { Input[j] = 0xa3; } byte[] Digest = H.ComputeVariable(Input); string s = Hashes.BinaryToString(Digest); Assert.AreEqual("79f38adec5c20307a98ef76e8324afbfd46cfd81b22e3973c65fa1bd9de31787", s); Assert.AreEqual(States1600Bits.Length, i); }
private byte[] GetSaltHash(string prefix) { SHA3_256 sHA3_256 = new SHA3_256(); return(sHA3_256.ComputeVariable( Encoding.ASCII.GetBytes(prefix + LIBRA_HASH_SUFFIX))); }
public static byte[] Hmac(this byte[] data, byte[] key, HashAlgorithm hashAlgorithm) { HMAC hmac; switch (hashAlgorithm) { case HashAlgorithm.SHA_1: hmac = new HMACSHA1(); hmac.Key = key; return(hmac.ComputeHash(data)); case HashAlgorithm.SHA_256: hmac = new HMACSHA256(); hmac.Key = key; return(hmac.ComputeHash(data)); case HashAlgorithm.SHA_384: hmac = new HMACSHA384(); hmac.Key = key; return(hmac.ComputeHash(data)); case HashAlgorithm.SHA_512: hmac = new HMACSHA512(); hmac.Key = key; return(hmac.ComputeHash(data)); default: hmac = new HMACSHA256(); hmac.Key = key; return(hmac.ComputeHash(data)); case HashAlgorithm.SHA3_256: var blockSize = 136; var ipad = new byte[blockSize]; var opad = new byte[blockSize]; if (key.Length > blockSize) { key = new SHA3_256().ComputeVariable(key); } else if (key.Length < blockSize) { var temp = new byte[blockSize]; Array.Copy(key, temp, key.Length); Array.Clear(temp, 128, blockSize - 128); key = temp; } for (var i = 0; i < blockSize; i++) { ipad[i] = i < key.Length ? (byte)(key[i] ^ 0x36) : (byte)0x36; opad[i] = i < key.Length ? (byte)(key[i] ^ 0x5C) : (byte)0x5C; } var hash1 = new SHA3_256().ComputeVariable(ipad.Concat(data).ToArray()); var hash2 = new SHA3_256().ComputeVariable(opad.Concat(hash1).ToArray()); return(hash2); } }
public void Test_02_SHA3_256() { SHA3_256 H = new SHA3_256(); byte[] Digest = H.ComputeVariable(new MemoryStream(new byte[0])); string s = Hashes.BinaryToString(Digest); Assert.AreEqual("a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a", s); }
public byte[] GetHash(byte[] rawBytes, EHashType eHashType) { SHA3_256 sHA3_256 = new SHA3_256(); _eHashType = eHashType; return(sHA3_256.ComputeVariable( GetSaltHash(GetSalt(_eHashType)) .Concat(rawBytes).ToArray())); }
/// <summary> /// Evaluates the function on a scalar argument. /// </summary> /// <param name="Argument">Function argument.</param> /// <param name="Variables">Variables collection.</param> /// <returns>Function result.</returns> public override IElement EvaluateScalar(IElement Argument, Variables Variables) { if (!(Argument.AssociatedObjectValue is byte[] Bin)) { throw new ScriptRuntimeException("Binary data expected.", this); } SHA3_256 H = new SHA3_256(); return(new ObjectValue(H.ComputeVariable(Bin))); }
public void Test_03_1600_bits_Stream() { SHA3_256 H = new SHA3_256(); byte[] Input = new byte[200]; int j; for (j = 0; j < 200; j++) { Input[j] = 0xa3; } byte[] Digest = H.ComputeVariable(new MemoryStream(Input)); string s = Hashes.BinaryToString(Digest); Assert.AreEqual("79f38adec5c20307a98ef76e8324afbfd46cfd81b22e3973c65fa1bd9de31787", s); }
// public async Task<bool> TransferCoins(Account sender, string receiverAddress, ulong amount, ulong gasUnitPrice = 0, ulong maxGasAmount = 1000000) public async Task <bool> TransferCoins(Account sender, string receiverAddress, ulong amount, ulong gasUnitPrice = 0, ulong maxGasAmount = 1000000) { try { var program = new Program(); program.Code = Convert.FromBase64String(Constant.ProgamBase64Codes.PeerToPeerTxn).ToByteString(); program.Arguments.Add(new TransactionArgument { Type = TransactionArgument.Types.ArgType.Address, Data = receiverAddress.ToByteString() }); program.Arguments.Add(new TransactionArgument { Type = TransactionArgument.Types.ArgType.U64, Data = amount.ToBytes().Reverse().ToByteString() }); var transaction = new RawTransaction(); transaction.ExpirationTime = (ulong)Math.Floor((decimal)DateTimeOffset.Now.ToUnixTimeMilliseconds() / 1000) + 100; transaction.GasUnitPrice = gasUnitPrice; transaction.MaxGasAmount = maxGasAmount; var accountState = await QueryBalance(sender.Address); transaction.SequenceNumber = accountState.SequenceNumber; transaction.Program = program; transaction.SenderAccount = sender.Address.ToByteString(); var hash = new SHA3_256().ComputeVariable(Constant.HashSaltValues.RawTransactionHashSalt.FromHexToBytes().Concat(transaction.ToByteArray()).ToArray()); var senderSignature = sender.KeyPair.Sign(hash); var request = new SubmitTransactionRequest { SignedTxn = new SignedTransaction { RawTxnBytes = transaction.ToByteString(), SenderPublicKey = sender.PublicKey, SenderSignature = senderSignature.ToByteString() } }; var response = await acClient.SubmitTransactionAsync(request); return(response.AcStatus.Code == AdmissionControlStatusCode.Accepted); } catch (Exception ex) { throw ex; } }
public void Test_01_0_bits() { SHA3_256 H = new SHA3_256(); int i = 0; H.NewState += (sender, e) => { string Expected = States0Bits[i++].Replace(" ", string.Empty); string Actual = Hashes.BinaryToString(H.GetState()).ToUpper(); Assert.AreEqual(Expected, Actual); }; byte[] Digest = H.ComputeVariable(new byte[0]); string s = Hashes.BinaryToString(Digest); Assert.AreEqual("a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a", s); Assert.AreEqual(States0Bits.Length, i); }
public void ConvertToUpperCasing() { if (!string.IsNullOrEmpty(MD5)) { MD5 = MD5.ToUpper(); } if (!string.IsNullOrEmpty(SHA1)) { SHA1 = SHA1.ToUpper(); } if (!string.IsNullOrEmpty(SHA256)) { SHA256 = SHA256.ToUpper(); } if (!string.IsNullOrEmpty(SHA384)) { SHA384 = SHA384.ToUpper(); } if (!string.IsNullOrEmpty(SHA512)) { SHA512 = SHA512.ToUpper(); } if (!string.IsNullOrEmpty(CRC32)) { CRC32 = CRC32.ToUpper(); } if (!string.IsNullOrEmpty(RIPEMD160)) { RIPEMD160 = RIPEMD160.ToUpper(); } if (!string.IsNullOrEmpty(SHA3_256)) { SHA3_256 = SHA3_256.ToUpper(); } if (!string.IsNullOrEmpty(SHA3_384)) { SHA3_384 = SHA3_384.ToUpper(); } if (!string.IsNullOrEmpty(SHA3_512)) { SHA3_512 = SHA3_512.ToUpper(); } }
public string HMACGenerator() { string keyedStr = String.Concat(this.Key, this.Args[this.AIMove - 1]); var hmacString = new StringBuilder(); SHA3_256 sha = new SHA3_256(); byte[] bytedValue = Encoding.UTF8.GetBytes(keyedStr); byte[] shaBytes = sha.ComputeVariable(bytedValue); foreach (var val in shaBytes) { hmacString.Append(val.ToString("x2")); } return(hmacString.ToString().ToUpper()); }
static void Main(string[] args) { TestCases tc = new TestCases(); for (int i = 0; i < 2; i++) { byte[] result = SHA3_224.ComputeHash(tc.SHA3_224[i].Input); if (CompareArrays(result, tc.SHA3_224[i].Result)) { Console.WriteLine("SHA3_224 TEST " + i + " -PASS-"); } else { Console.WriteLine("SHA3_224 TEST " + i + " *FAIL!*"); } result = SHA3_256.ComputeHash(tc.SHA3_256[i].Input); if (CompareArrays(result, tc.SHA3_256[i].Result)) { Console.WriteLine("SHA3_256 TEST " + i + " -PASS-"); } else { Console.WriteLine("SHA3_256 TEST " + i + " *FAIL!*"); } result = SHA3_384.ComputeHash(tc.SHA3_384[i].Input); if (CompareArrays(result, tc.SHA3_384[i].Result)) { Console.WriteLine("SHA3_384 TEST " + i + " -PASS-"); } else { Console.WriteLine("SHA3_384 TEST " + i + " *FAIL!*"); } result = SHA3_512.ComputeHash(tc.SHA3_512[i].Input); if (CompareArrays(result, tc.SHA3_512[i].Result)) { Console.WriteLine("SHA3_512 TEST " + i + " -PASS-"); } else { Console.WriteLine("SHA3_512 TEST " + i + " *FAIL!*"); } result = SHAKE128.ComputeHash(tc.SHAKE128[i].Input, tc.SHAKE128[i].Result.Length); if (CompareArrays(result, tc.SHAKE128[i].Result)) { Console.WriteLine("SHAKE128 TEST " + i + " -PASS-"); } else { Console.WriteLine("SHAKE128 TEST " + i + " *FAIL!*"); } result = SHAKE256.ComputeHash(tc.SHAKE256[i].Input, tc.SHAKE256[i].Result.Length); if (CompareArrays(result, tc.SHAKE256[i].Result)) { Console.WriteLine("SHAKE256 TEST " + i + " -PASS-"); } else { Console.WriteLine("SHAKE256 TEST " + i + " *FAIL!*"); } } Console.WriteLine("Press ENTER to exit..."); Console.ReadLine(); }
public async Task <bool> TransferCoins(Account sender, string receiverAddress, ulong amount, ulong gasUnitPrice = 0, ulong maxGasAmount = 1000000) { try { var accountState = await QueryBalance(sender.Address); var program = new ProgramLCS(); program.Code = Convert.FromBase64String(Constant.ProgamBase64Codes.PeerToPeerTxn); program.TransactionArguments = new List <TransactionArgumentLCS>(); program.TransactionArguments.Add(new TransactionArgumentLCS { ArgType = Types.TransactionArgument.Types.ArgType.Address, Address = new AddressLCS { Value = receiverAddress } }); program.TransactionArguments.Add(new TransactionArgumentLCS { ArgType = Types.TransactionArgument.Types.ArgType.U64, U64 = amount }); program.Modules = new List <byte[]>(); var transaction = new RawTransactionLCS { Sender = new AddressLCS { Value = sender.Address }, SequenceNumber = accountState.SequenceNumber, TransactionPayload = new TransactionPayloadLCS { PayloadType = TransactionPayloadType.Program, Program = program }, MaxGasAmount = maxGasAmount, GasUnitPrice = gasUnitPrice, ExpirationTime = (ulong)Math.Floor((decimal)DateTimeOffset.Now.ToUnixTimeMilliseconds() / 1000) + 100 }; var transactionLCS = LCSCore.LCSDeserialization(transaction); var digestSHA3 = new SHA3_256(); var saltDigest = digestSHA3.ComputeVariable(Constant.HashSaltValues.RawTransactionHashSalt.ToBytes()); var saltDigestAndTransaction = saltDigest.Concat(transactionLCS).ToArray(); var hash = digestSHA3.ComputeVariable(saltDigestAndTransaction); var senderSignature = sender.KeyPair.Sign(hash); var publicKeyLen = BitConverter.GetBytes((uint)sender.PublicKey.Length); var signatureLen = BitConverter.GetBytes((uint)senderSignature.Length); var txnBytes = transactionLCS.Concat(publicKeyLen).ToArray(); txnBytes = txnBytes.Concat(sender.PublicKey).ToArray(); txnBytes = txnBytes.Concat(signatureLen).ToArray(); txnBytes = txnBytes.Concat(senderSignature).ToArray(); var request = new SubmitTransactionRequest { SignedTxn = new SignedTransaction { TxnBytes = txnBytes.ToByteString() } }; var response = await acClient.SubmitTransactionAsync(request); return(response.AcStatus.Code == AdmissionControlStatusCode.Accepted); } catch (Exception ex) { throw ex; } }