/// <summary> /// Hashes the specified payload. /// </summary> /// <param name="payload">The payload.</param> /// <param name="size">The size.</param> /// <returns>System.Byte[].</returns> public byte[] Hash(byte[] payload, int size) { var nKey = new byte[size]; //using (var random = new RNGCryptoServiceProvider()) // random.GetBytes(nKey); RandomNumberGenerator.Create().GetBytes(nKey); // Using Paseto Cryptography library using (var hash = new Blake2B()) return(hash.ComputeHash(nKey)); /* * Using NSec library * * var algo = new Blake2bMac(); * using (var key = Key.Import(algo, nKey, KeyBlobFormat.RawSymmetricKey)) * return algo.Mac(key, payload, size); */ // Using Sodium Core library //var hash = new GenericHash.GenericHashAlgorithm(nKey, size); //return hash.ComputeHash(GetBytes(payload)); }
/* * bool Proof::Test() * { * uint32_t input[SEED_LENGTH + 2]; * for (unsigned i = 0; i < SEED_LENGTH; ++i) * input[i] = seed[i]; * input[SEED_LENGTH] = nonce; * input[SEED_LENGTH + 1] = 0; * uint32_t buf[MAX_N / 4]; * std::vector<uint32_t> blocks(k + 1, 0); * for (unsigned i = 0; i < inputs.size(); ++i) * { * input[SEED_LENGTH + 1] = inputs[i]; * blake2b((uint8_t*)buf, &input, NULL, sizeof(buf), sizeof(input), 0); * for (unsigned j = 0; j < (k + 1); ++j) * { * //select j-th block of n/(k+1) bits * blocks[j] ^= buf[j] >> (32 - n / (k + 1)); * } * } * bool b = true; * for (unsigned j = 0; j < (k + 1); ++j) * { * b &= (blocks[j] == 0); * } * if (b && inputs.size() != 0) * { * printf("Solution found:\n"); * for (unsigned i = 0; i < inputs.size(); ++i) * { * printf(" %x ", inputs[i]); * } * printf("\n"); * } * return b; * } */ public bool Test() { uint[] input = new uint[Equihash.SEED_LENGTH + 2]; for (uint i = 0; i < Equihash.SEED_LENGTH; ++i) { input[i] = seed[i]; } input[Equihash.SEED_LENGTH] = nonce; input[Equihash.SEED_LENGTH + 1] = 0; uint[] buf = new uint[Equihash.MAX_N / 4]; List <uint> blocks = new List <uint>(); for (int i = 0; i < k + 1; i++) { blocks.Add(0); } for (int i = 0; i < inputs.Count; ++i) { input[Equihash.SEED_LENGTH + 1] = inputs[i]; byte[] inputBytes; using (MemoryStream ms = new MemoryStream()) { for (int x = 0; x < input.Length; x++) { ms.Write(BitConverter.GetBytes(input[x]), 0, 4); } inputBytes = ms.ToArray(); } Blake2B blake2b = new Blake2B(256); byte[] result = blake2b.ComputeHash(inputBytes); for (int x = 0; x < buf.Length; x++) { buf[x] = BitConverter.ToUInt32(result, x * 4); } for (int j = 0; j < (k + 1); ++j) { //select j-th block of n/(k+1) bits blocks[j] = blocks[j] ^ (buf[j] >> (int)(32 - n / (k + 1))); } } bool b = true; for (int j = 0; j < (k + 1); ++j) { b = (blocks[j] == 0); } if (b && inputs.Count != 0) { Console.WriteLine("Solution found:"); for (int i = 0; i < inputs.Count; ++i) { Console.Write($" {inputs[i]} "); } Console.WriteLine(""); } return(b); }
/// <summary>Blake2 hashed the specified bytes.</summary> /// <param name="bytes">The bytes.</param> /// <param name="size">The size.</param> /// <param name="key">The key.</param> /// <returns> /// <br /> /// </returns> public static byte[] Blake2(byte[] bytes, int size = 128, IReadOnlyList <byte> key = null) { var config = new Blake2BConfig { OutputSizeInBits = size, Key = null }; return(Blake2B.ComputeHash(bytes, config)); }
public string CalculateMessageNetworkId() { var networkIdBytes = Blake2B.ComputeHash(Encoding.ASCII.GetBytes(this.NetworkId), new Blake2BConfig { OutputSizeInBytes = 32 }, null); return(BitConverter.ToInt64(networkIdBytes.Take(8).ToArray(), 0).ToString()); }
/// <summary>Blake2 hashed with bytes concated at the end.</summary> /// <param name="bytes">The bytes.</param> /// <param name="size">The size.</param> /// <returns> /// <br /> /// </returns> public static byte[] Blake2Concat(byte[] bytes, int size = 128) { var config = new Blake2BConfig { OutputSizeInBits = size, Key = null }; return(Blake2B.ComputeHash(bytes, config).Concat(bytes).ToArray()); }
public byte[] Blake2b(byte[] input) { var blakeConfig = new Blake2BConfig { OutputSizeInBits = 256 }; return(Blake2B.ComputeHash(input, 0, input.Count(), blakeConfig)); }
public static byte[] FastHash(byte[] message, int offset, int length) { var blakeConfig = new Blake2BConfig { OutputSizeInBits = 256 }; return(Blake2B.ComputeHash(message, offset, length, blakeConfig)); }
public static byte[] SecureHash(byte[] message, int offset, int lenght) { var blakeConfig = new Blake2BConfig { OutputSizeInBits = 256 }; var blake2B = Blake2B.ComputeHash(message, offset, lenght, blakeConfig); return(Hash(blake2B, 0, blake2B.Length, Keccak256)); }
private static byte[] CalculateBlake2StrongSum(byte[] block) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Expected O, but got Unknown Blake2BConfig val = new Blake2BConfig(); val.set_OutputSizeInBytes(32); return(Blake2B.ComputeHash(block, val)); }
// Generate blake2b nonce with clientkey(pk) and serverkey. private static byte[] GenerateBlake2BNonce(byte[] clientKey, byte[] serverKey) { var hashBuffer = new byte[clientKey.Length + serverKey.Length]; Buffer.BlockCopy(clientKey, 0, hashBuffer, 0, clientKey.Length); Buffer.BlockCopy(serverKey, 0, hashBuffer, PublicKeyBox.PublicKeyLength, serverKey.Length); using (var blake = new Blake2B(24)) return(blake.ComputeHash(hashBuffer)); }
static readonly byte[] personalization = new byte[] { 99, 107, 98, 45, 100, 101, 102, 97, 117, 108, 116, 45, 104, 97, 115, 104 }; // ckb-default-hash public static byte[] ComputeHash(byte[] data) { Blake2BConfig config = new Blake2BConfig { Personalization = personalization, OutputSizeInBytes = 32 }; SecureArrayCall secureArrayCall = default; return(Blake2B.ComputeHash(data, config, secureArrayCall)); }
/// <inheritdoc /> public long DoPow(byte[] message, int targetScore) { var relevantMessagePart = message.Take(message.Length - 8).ToArray(); var digest = Blake2B.ComputeHash(relevantMessagePart, new Blake2BConfig { OutputSizeInBytes = 32 }, null); var targetZeros = (int)Math.Ceiling(Math.Log((relevantMessagePart.Length + 8) * targetScore) / this.LN3); return(this.DoWork(digest, targetZeros)); }
/// <summary> /// get the Blake2b-512 encrypt /// </summary> /// <param name="byteArrayToEncrypt">Byte array to encrypt</param> /// <returns></returns> public static string Blake2b_512Hash(this byte[] byteArrayToEncrypt) { StringBuilder sBuilder = new StringBuilder(); foreach (var encryptedByte in Blake2B.ComputeHash(byteArrayToEncrypt)) { sBuilder.Append(encryptedByte.ToString("x2")); } return(sBuilder.ToString()); }
public void CheckTestVectors() { for (int len = 0; len < TestVectors.UnkeyedBlake2B.Length; len++) { var input = Enumerable.Range(0, len).Select(i => (byte)i).ToArray(); var hash = Blake2B.ComputeHash(input); string actual = BitConverter.ToString(hash).Replace("-", ""); string expected = TestVectors.UnkeyedBlake2B[len]; Assert.AreEqual(expected, actual); } }
public static Ed25519Address FromPublicKey(byte[] publicKey) { var addressHash = Blake2B.ComputeHash(publicKey, new Blake2BConfig { OutputSizeInBytes = 32 }, null); return(new Ed25519Address { Address = addressHash.ToHex(), PublicKey = publicKey }); }
public static string getBlake2b(string text) { byte[] bytes = Encoding.UTF8.GetBytes(text); Blake2B hashstring = new Blake2B(); byte[] hash = hashstring.ComputeHash(bytes); string hashString = string.Empty; foreach (byte x in hash) { hashString += String.Format("{0:x2}", x); } return(hashString); }
private void SignTransfer() { IsBusy = true; try { if (!App.IsPolkadotApiConnected) { App.ConnectToNode(); } _ = Task.Run(async() => { try { var sender = new Address("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"); //var pub = AddressUtils.GetPublicKeyFromAddr(sender); var secret = "0x33A6F3093F158A7109F679410BEF1A0C54168145E0CECB4DF006C1C2FFFB1F09925A225D97AA00682D6A59B95B18780C10D7032336E88F3442B42361F4A66011"; var recipient = "5Ef1wcrhb5CVyZjpdYh9Keg81tgUPcsYi9uhNHC9uqjo7956"; var amount = BigInteger.Parse("10"); var tmp = Blake2B.ComputeHash(secret.HexToByteArray()); var tcs = new TaskCompletionSource <string>(); var sid = PolkadotApi.SignAndSendTransfer(sender.Symbols, secret, recipient, amount, res => tcs.SetResult(res)); Trace.WriteLine(sid); var result = await tcs.Task.WithTimeout(TimeSpan.FromSeconds(30)); PolkadotApi.UnsubscribeStorage(sid); Trace.WriteLine(result); } catch (Exception ex) { Trace.WriteLine(ex); } }); } catch (System.Exception ex) { Trace.WriteLine(ex); } finally { IsBusy = false; } }
/// <summary> /// Does a Blake2 hash with the ability to truncate or extend the hash to any length. /// </summary> /// <param name="hash"> /// The buffer to fill with the hash. /// </param> /// <param name="inputBuffer"> /// What to hash. /// </param> /// <param name="secureArrayCall"> /// The methods that get called to secure arrays. A null value defaults to <see cref="SecureArray"/>.<see cref="SecureArray.DefaultCall"/>. /// </param> private static void Blake2BLong(byte[] hash, byte[] inputBuffer, SecureArrayCall secureArrayCall) { var outputLengthBytes = new byte[4]; using var intermediateHash = SecureArray <byte> .Best(Blake2B.OutputLength, secureArrayCall); var config = new Blake2BConfig { Result64ByteBuffer = intermediateHash.Buffer, OutputSizeInBytes = hash.Length > 64 ? 64 : hash.Length, }; Store32(outputLengthBytes, hash.Length); using (var blakeHash = Blake2B.Create(config, secureArrayCall)) { blakeHash.Update(outputLengthBytes); blakeHash.Update(inputBuffer); blakeHash.Finish(); } if (hash.Length <= intermediateHash.Buffer.Length) { Array.Copy(intermediateHash.Buffer, hash, hash.Length); return; } const int b2B2 = Blake2B.OutputLength / 2; Array.Copy(intermediateHash.Buffer, hash, b2B2); int pos = b2B2; int lastHashIndex = hash.Length - Blake2B.OutputLength; var toHash = new byte[Blake2B.OutputLength]; while (pos < lastHashIndex) { Array.Copy(intermediateHash.Buffer, toHash, intermediateHash.Buffer.Length); Blake2B.ComputeHash(toHash, config, secureArrayCall); Array.Copy(intermediateHash.Buffer, 0, hash, pos, b2B2); pos += b2B2; } Array.Copy(intermediateHash.Buffer, toHash, intermediateHash.Buffer.Length); Blake2B.ComputeHash(toHash, config, secureArrayCall); Array.Copy(intermediateHash.Buffer, 0, hash, pos, hash.Length - pos); }
/* * void Equihash::FillMemory(uint32_t length) //works for k<=7 * { * uint32_t input[SEED_LENGTH + 2]; * for (unsigned i = 0; i < SEED_LENGTH; ++i) * input[i] = seed[i]; * input[SEED_LENGTH] = nonce; * input[SEED_LENGTH + 1] = 0; * uint32_t buf[MAX_N / 4]; * for (unsigned i = 0; i < length; ++i, ++input[SEED_LENGTH + 1]) { * blake2b((uint8_t*)buf, &input, NULL, sizeof(buf), sizeof(input), 0); * uint32_t index = buf[0] >> (32 - n / (k + 1)); * unsigned count = filledList[index]; * if (count < LIST_LENGTH) { * for (unsigned j = 1; j < (k + 1); ++j) { * //select j-th block of n/(k+1) bits * tupleList[index][count].blocks[j - 1] = buf[j] >> (32 - n / (k + 1)); * } * tupleList[index][count].reference = i; * filledList[index]++; * } * } * } */ public void FillMemory(ulong length) //works for k<=7 { uint[] input = new uint[Equihash.SEED_LENGTH + 2]; for (uint i = 0; i < SEED_LENGTH; ++i) { input[i] = seed[i]; } input[SEED_LENGTH] = nonce; input[SEED_LENGTH + 1] = 0; uint[] buf = new uint[Equihash.MAX_N / 4]; for (uint i = 0; i < length; ++i, ++input[SEED_LENGTH + 1]) { byte[] inputBytes; using (MemoryStream ms = new MemoryStream()) { for (int x = 0; x < input.Length; x++) { ms.Write(BitConverter.GetBytes(input[x]), 0, 4); } inputBytes = ms.ToArray(); } Blake2B blake2b = new Blake2B(256); byte[] result = blake2b.ComputeHash(inputBytes); for (int x = 0; x < buf.Length; x++) { buf[x] = BitConverter.ToUInt32(result, x * 4); } uint index = buf[0] >> (int)(32 - n / (k + 1)); uint count = filledList[index]; if (count < LIST_LENGTH) { for (uint j = 1; j < (k + 1); ++j) { //select j-th block of n/(k+1) bits tupleList[index, count, j - 1] = buf[j] >> (int)(32 - n / (k + 1)); } tupleList[index, count, REFERENCE_BLOCK_INDEX] = i; filledList[index]++; } } }
public static string GetHashStringBlacke2b(this string clearstring) { return(Convert.ToBase64String(Blake2B.ComputeHash(Blake2B.ComputeHash(Encoding.UTF8.GetBytes(clearstring))))); }
public byte[] CalculateHash() { return(Blake2B.ComputeHash(this.Serialize(), new Blake2BConfig { OutputSizeInBytes = 32 }, null)); }
public static string ApplyBlacke2(this string data) { var p = Blake2B.ComputeHash(System.Text.Encoding.UTF8.GetBytes(data)); return(Convert.ToBase64String(p)); }
/// <summary>Blake2 hashed the specified ss prefixed.</summary> /// <param name="ssPrefixed">The ss prefixed.</param> /// <param name="start">The start.</param> /// <param name="count">The count.</param> /// <returns> /// <br /> /// </returns> internal static byte[] Blake2(byte[] ssPrefixed, int start, int count) { return(Blake2B.ComputeHash(ssPrefixed, start, count)); }
private static string Blake2b(string data) { return(Convert.ToBase64String(Blake2B.ComputeHash(Encoding.UTF8.GetBytes(data)))); }