/// <summary> /// Gets AES Initiation Vector from stanza attributes.s /// </summary> /// <param name="Id">Id attribute</param> /// <param name="Type">Type attribute</param> /// <param name="From">From attribute</param> /// <param name="To">To attribute</param> /// <returns>AES Initiation vector.</returns> protected byte[] GetIV(string Id, string Type, string From, string To) { byte[] IV = Hashes.ComputeSHA256Hash(Encoding.UTF8.GetBytes(Id + Type + From + To)); Array.Resize <byte>(ref IV, 16); return(IV); }
public void Test_21_X448_ECDH() { byte[] A = Hashes.StringToBinary("9a8f4925d1519f5775cf46b04b5800d4ee9ee8bae8bc5565d498c28dd9c9baf574a9419744897391006382a6f127ab1d9ac2d8c0a598726b"); Curve448 Alice = new Curve448(A); Assert.AreEqual("9b08f7cc31b7e3e67d22d5aea121074a273bd2b83de09c63faa73d2c22c5d9bbc836647241d953d40c5b12da88120d53177f80e532c41fa000", Hashes.BinaryToString(Alice.PublicKey)); A = Hashes.StringToBinary("1c306a7ac2a0e2e0990b294470cba339e6453772b075811d8fad0d1d6927c120bb5ee8972b0d3e21374c9c921b09d1b0366f10b65173992d"); Curve448 Bob = new Curve448(A); Assert.AreEqual("3eb7a829b0cd20f5bcfc0b599b6feccf6da4627107bdb0d4f345b43027d8b972fc3e34fb4232a13ca706dcb57aec3dae07bdc1c67bf33609", Hashes.BinaryToString(Bob.PublicKey)); byte[] Key1 = Alice.GetSharedKey(Bob.PublicKey, Hashes.ComputeSHA256Hash); byte[] Key2 = Bob.GetSharedKey(Alice.PublicKey, Hashes.ComputeSHA256Hash); string k1 = Hashes.BinaryToString(Key1); string k2 = Hashes.BinaryToString(Key2); Assert.AreEqual(k1, k2); A = Hashes.StringToBinary("07fff4181ac6cc95ec1c16a94a0f74d12da232ce40a77552281d282bb60c0b56fd2464c335543936521c24403085d59a449a5037514a879d"); if (A.Length != 56) { Array.Resize <byte>(ref A, 56); } Array.Reverse(A); // Most significant byte first. A = Hashes.ComputeSHA256Hash(A); string k3 = Hashes.BinaryToString(A); Assert.AreEqual(k1, k3); }
public void Test_15_X25519_ECDH() { byte[] A = Hashes.StringToBinary("77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a"); Curve25519 Alice = new Curve25519(A); Assert.AreEqual("8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a", Hashes.BinaryToString(Alice.PublicKey)); A = Hashes.StringToBinary("5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb"); Curve25519 Bob = new Curve25519(A); Assert.AreEqual("de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f", Hashes.BinaryToString(Bob.PublicKey)); byte[] Key1 = Alice.GetSharedKey(Bob.PublicKey, Hashes.ComputeSHA256Hash); byte[] Key2 = Bob.GetSharedKey(Alice.PublicKey, Hashes.ComputeSHA256Hash); string k1 = Hashes.BinaryToString(Key1); string k2 = Hashes.BinaryToString(Key2); Assert.AreEqual(k1, k2); A = Hashes.StringToBinary("4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742"); if (A.Length != 32) { Array.Resize <byte>(ref A, 32); } Array.Reverse(A); // Most significant byte first. A = Hashes.ComputeSHA256Hash(A); string k3 = Hashes.BinaryToString(A); Assert.AreEqual(k1, k3); }
/// <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); } return(new ObjectValue(Hashes.ComputeSHA256Hash(Bin))); }
/// <summary> /// Computes a hash of a password. /// </summary> /// <param name="UserName">User name.</param> /// <param name="Password">Password.</param> public static byte[] ComputeHash(string UserName, string Password) { HashComputationMethod h = hashMethod; if (h is null) { return(Hashes.ComputeSHA256Hash(Encoding.UTF8.GetBytes(UserName + ":" + Password))); } else { return(h(UserName, Password)); } }
/// <summary> /// Calculates a private key from a secret. /// </summary> /// <param name="Secret">Binary secret.</param> /// <returns>Private key</returns> public override Tuple <byte[], byte[]> CalculatePrivateKey(byte[] Secret) { byte[] Bin = Secret; if (Bin.Length != 32) { Bin = Hashes.ComputeSHA256Hash(Secret); } Bin[0] &= 0xf8; Bin[31] &= 0x3f; Bin[31] |= 0x40; return(new Tuple <byte[], byte[]>(Bin, null)); }
/// <summary> /// Gets an Initiation Vector from stanza attributes. /// </summary> /// <param name="Id">Id attribute</param> /// <param name="Type">Type attribute</param> /// <param name="From">From attribute</param> /// <param name="To">To attribute</param> /// <param name="Counter">Counter. Can be reset every time a new key is generated. /// A new key must be generated before the counter wraps.</param> /// <returns>Initiation vector.</returns> protected override byte[] GetIV(string Id, string Type, string From, string To, uint Counter) { byte[] IV = Hashes.ComputeSHA256Hash(Encoding.UTF8.GetBytes(Id + Type + From + To)); Array.Resize <byte>(ref IV, 12); IV[8] = (byte)Counter; Counter >>= 8; IV[9] = (byte)Counter; Counter >>= 8; IV[10] = (byte)Counter; Counter >>= 8; IV[11] = (byte)Counter; return(IV); }
public async Task <KeyValuePair <byte[], byte[]> > GetCustomKey(string fileName) { byte[] key; byte[] iv; string s; int i; try { s = await SecureStorage.GetAsync(fileName); } catch (TypeInitializationException ex) { this.logService.LogException(ex); // No secure storage available. key = Hashes.ComputeSHA256Hash(Encoding.UTF8.GetBytes(fileName + ".Key")); iv = Hashes.ComputeSHA256Hash(Encoding.UTF8.GetBytes(fileName + ".IV")); Array.Resize <byte>(ref iv, 16); return(new KeyValuePair <byte[], byte[]>(key, iv)); } if (!string.IsNullOrWhiteSpace(s) && (i = s.IndexOf(',')) > 0) { key = Hashes.StringToBinary(s.Substring(0, i)); iv = Hashes.StringToBinary(s.Substring(i + 1)); } else { key = new byte[32]; iv = new byte[16]; lock (rnd) { rnd.GetBytes(key); rnd.GetBytes(iv); } s = Hashes.BinaryToString(key) + "," + Hashes.BinaryToString(iv); await SecureStorage.SetAsync(fileName, s); } return(new KeyValuePair <byte[], byte[]>(key, iv)); }
/// <summary> /// Hash function /// </summary> /// <param name="Data">Data to hash.</param> /// <returns>Hash of data.</returns> public override byte[] H(byte[] Data) { return(Hashes.ComputeSHA256Hash(Data)); }