public void Test_CryptoUtils_Pbkdf2Sha_1() { byte[] saltBytes = SharedUtils.HexStringToByteArray("4125c247e43ab1e93c6dff76"); byte[] saltedPassword = CryptoUtils.pbkdf2Sha("pencil".Normalize(), saltBytes, 4096, HashAlgorithmName.SHA1, 20); byte[] saltedPasswordRef = SharedUtils.HexStringToByteArray("1d96ee3a529b5a5f9e47c01f229a2cb8a6e15f7d"); Assert.IsTrue(saltedPassword.SequenceEqual(saltedPasswordRef)); }
public void Test_CryptoUtils_hash() { byte[] clientKey = SharedUtils.HexStringToByteArray("e234c47bf6c36696dd6d852b99aaa2ba26555728"); byte[] storedKey = CryptoUtils.hash(clientKey, HashAlgorithmNames.Sha1); byte[] storedKeyRef = SharedUtils.HexStringToByteArray("e9d94660c39d65c38fbad91c358f14da0eef2bd6"); Assert.IsTrue(storedKey.SequenceEqual(storedKeyRef)); }
public void Test_CryptoUtils_HmacSha1_5() { // Values vectors from: https://tools.ietf.org/html/rfc2202 byte[] key = SharedUtils.HexStringToByteArray("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"); byte[] data = Encoding.UTF8.GetBytes("Hi There"); byte[] digest = CryptoUtils.hmacSha1(data, key); byte[] digestRef = SharedUtils.HexStringToByteArray("b617318655057264e28bc0b6fb378c8ef146be00"); Assert.IsTrue(digest.SequenceEqual(digestRef)); }
public void Test_CryptoUtils_HmacSha1_4() { byte[] serverKey = SharedUtils.HexStringToByteArray("0fe09258b3ac852ba502cc62ba903eaacdbf7d31"); string authMessage = "n=user,r=fyko+d2lbbFgONRv9qkxdawL,r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,s=QSXCR+Q6sek8bf92,i=4096,c=biws,r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j"; byte[] serverSignature = CryptoUtils.hmacSha1(authMessage, serverKey); byte[] serverSignatureRef = SharedUtils.HexStringToByteArray("ae617da6a57c4bbb2e0286568dae1d251905b0a4"); Assert.IsTrue(serverSignature.SequenceEqual(serverSignatureRef)); }
public void Test_CryptoUtils_HmacSha1_3() { byte[] saltedPassword = SharedUtils.HexStringToByteArray("1d96ee3a529b5a5f9e47c01f229a2cb8a6e15f7d"); string authMessage = "Server Key"; byte[] serverKey = CryptoUtils.hmacSha1(authMessage, saltedPassword); byte[] serverKeyRef = SharedUtils.HexStringToByteArray("0fe09258b3ac852ba502cc62ba903eaacdbf7d31"); Assert.IsTrue(serverKey.SequenceEqual(serverKeyRef)); }
public void Test_CryptoUtils_HmacSha1_2() { byte[] storedKey = SharedUtils.HexStringToByteArray("e9d94660c39d65c38fbad91c358f14da0eef2bd6"); string authMessage = "n=user,r=fyko+d2lbbFgONRv9qkxdawL,r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,s=QSXCR+Q6sek8bf92,i=4096,c=biws,r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j"; byte[] clientSignature = CryptoUtils.hmacSha1(authMessage, storedKey); byte[] clientSignatureRef = SharedUtils.HexStringToByteArray("5d7138c486b0bfabdf49e3e2da8bd6e5c79db613"); Assert.IsTrue(clientSignature.SequenceEqual(clientSignatureRef)); }
public void Test_CryptoUtils_HmacSha1_1() { byte[] saltedPassword = SharedUtils.HexStringToByteArray("1d96ee3a529b5a5f9e47c01f229a2cb8a6e15f7d"); string authMessage = "Client Key"; byte[] clientKey = CryptoUtils.hmacSha1(authMessage, saltedPassword); byte[] clientKeyRef = SharedUtils.HexStringToByteArray("e234c47bf6c36696dd6d852b99aaa2ba26555728"); Assert.IsTrue(clientKey.SequenceEqual(clientKeyRef)); }
public void Test_CryptoUtils_ByteArrayHexString() { string input = "e234c47bf6c36696dd6d852b99aaa2ba26555728"; byte[] a = SharedUtils.HexStringToByteArray(input); string output = CryptoUtils.byteArrayToHexString(a); Assert.IsTrue(input.Equals(output)); }
public void Test_CryptoUtils_Xor() { byte[] clientKey = SharedUtils.HexStringToByteArray("e234c47bf6c36696dd6d852b99aaa2ba26555728"); byte[] clientSignature = SharedUtils.HexStringToByteArray("5d7138c486b0bfabdf49e3e2da8bd6e5c79db613"); byte[] clientProof = CryptoUtils.xor(clientKey, clientSignature); byte[] clientProofRef = SharedUtils.HexStringToByteArray("bf45fcbf7073d93d022466c94321745fe1c8e13b"); Assert.IsTrue(clientProof.SequenceEqual(clientProofRef)); }
public void Test_CryptoUtils_GenOmemoFingerprint() { string publicIdentKeySerializedHex = "3b6a27bcceb6a42d62a3a8d02a6f0d73653215771de243a63ac048a18b59da29"; byte[] publicIdentKeySerialized = SharedUtils.HexStringToByteArray(publicIdentKeySerializedHex); ECPubKeyModel identKeyPair = new ECPubKeyModel(publicIdentKeySerialized); string outputRef = "3b6a27bc ceb6a42d 62a3a8d0 2a6f0d73 65321577 1de243a6 3ac048a1 8b59da29"; string output = CryptoUtils.generateOmemoFingerprint(identKeyPair.key); Assert.AreEqual(outputRef, output); }
public void Test_CryptoUtils_HexToByteArray() { string saltStringBase64Ref = "QSXCR+Q6sek8bf92"; string saltStringHexRef = "4125c247e43ab1e93c6dff76"; byte[] salt = Convert.FromBase64String(saltStringBase64Ref); byte[] saltRef = SharedUtils.HexStringToByteArray(saltStringHexRef); Assert.IsTrue(salt.SequenceEqual(saltRef)); string saltStringHex = CryptoUtils.byteArrayToHexString(salt); Assert.IsTrue(saltStringHex.Equals(saltStringHexRef)); }
public void Test_SignPreKey() { ECPrivKeyModel priv = new ECPrivKeyModel(SharedUtils.HexStringToByteArray("1498b5467a63dffa2dc9d9e069caf075d16fc33fdd4c3b01bfadae6433767d93")); ECPubKeyModel pub = new ECPubKeyModel(SharedUtils.HexStringToByteArray("b7a3c12dc0c8c748ab07525b701122b88bd78f600c76342d27f25e5f92444cde")); IdentityKeyPairModel identityKeyPair = new IdentityKeyPairModel(priv, pub); for (uint id = 1; id < 250; id++) { byte[] data = Encoding.ASCII.GetBytes("Message for Ed25519 signing"); byte[] signature = new byte[Ed25519.SignatureSize]; Ed25519.Sign(identityKeyPair.privKey.key, 0, data, 0, data.Length, signature, 0); byte[] sigRef = SharedUtils.HexStringToByteArray("6dd355667fae4eb43c6e0ab92e870edb2de0a88cae12dbd8591507f584fe4912babff497f1b8edf9567d2483d54ddc6459bea7855281b7a246a609e3001a4e08"); string sigRefBase64 = Convert.ToBase64String(sigRef); string sigBase64 = Convert.ToBase64String(signature); Assert.AreEqual(sigBase64, sigRefBase64); } }
/// <summary> /// [WIP]<para/> /// Parses XMPP IRIs and URIs based on RFC 5122 and returns the result. /// </summary> /// <param name="uri">The URI or IRI that should get parsed.</param> /// <returns>The URI or IRI result or null if an error occurred.</returns> public static IUriAction parse(Uri uri) { if (!string.IsNullOrEmpty(uri?.OriginalString)) { if (string.Equals(uri.Scheme.ToLowerInvariant(), "xmpp")) { string tmp = uri.OriginalString; // 1. remove 'xmpp:' tmp = tmp.Substring(5); // 2. Authority string authority = null; if (tmp.StartsWith("//")) { tmp.Substring(2); int authEnd = tmp.IndexOf('/'); if (authEnd < 0) { authEnd = tmp.IndexOf('?'); if (authEnd < 0) { authEnd = tmp.IndexOf('#'); if (authEnd < 0) { authEnd = tmp.Length <= 0 ? 0 : tmp.Length - 1; } } authority = tmp.Substring(0, authEnd); tmp = tmp.Substring(authEnd + 1); } } if (string.Equals(uri.AbsolutePath, "iot-register")) { WwwFormUrlDecoder query = parseUriQuery(uri); if (query is null) { return(null); } IWwwFormUrlDecoderEntry macEntry = query.FirstOrDefault(x => x.Name.StartsWith("mac")); if (macEntry is null || string.IsNullOrEmpty(macEntry.Value)) { Logger.Error("None or invalid IoT MAC address: " + uri.OriginalString); return(null); } IWwwFormUrlDecoderEntry algoEntry = query.FirstOrDefault(x => x.Name.StartsWith("algo")); if (algoEntry is null || string.IsNullOrEmpty(algoEntry.Value)) { Logger.Error("None or invalid IoT key algorithm: " + uri.OriginalString); return(null); } IWwwFormUrlDecoderEntry keyEntry = query.FirstOrDefault(x => x.Name.StartsWith("key")); if (keyEntry is null || string.IsNullOrEmpty(keyEntry.Value)) { Logger.Error("None or invalid IoT key: " + uri.OriginalString); return(null); } return(new RegisterIoTUriAction(macEntry.Value, algoEntry.Value, keyEntry.Value)); } else { // Check if is OMEMO fingerprint URI: WwwFormUrlDecoder query = parseUriQuery(uri); if (query is null) { return(null); } IWwwFormUrlDecoderEntry entry = query.FirstOrDefault(x => x.Name.StartsWith("omemo-sid-")); if (!(entry is null)) { ECPubKeyModel pubKey = null; try { byte[] fingerprintBytes = SharedUtils.HexStringToByteArray(entry.Value); pubKey = new ECPubKeyModel(fingerprintBytes); } catch (Exception e) { Logger.Error("Failed to parse XMPP URI. Parsing fingerprint failed: " + entry.Value, e); return(null); } if (uint.TryParse(entry.Name.Replace("omemo-sid-", "").Trim(), out uint deviceId)) { OmemoProtocolAddress address = new OmemoProtocolAddress(uri.LocalPath, deviceId); return(new OmemoFingerprintUriAction(new OmemoFingerprint(pubKey, address))); } else { Logger.Warn("Failed to parse XMPP URI. Invalid device ID: " + entry.Name); } } } } else { Logger.Warn("Failed to parse XMPP URI. No 'xmpp' scheme."); } } return(null); }