public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { if (value is string item) { return(NetworkConverter.FromBase64UrlString(item)); } return(Array.Empty <byte>()); }
private static Stream FromBase64String(string value) { var match = _base64Regex.Match(value); if (!match.Success) { throw new ArgumentException(); } value = match.Groups[1].Value; return(new MemoryStream(NetworkConverter.FromBase64UrlString(value))); }
public void Test_NetworkConverter() { Assert.IsTrue(NetworkConverter.ToHexString(new byte[] { 0x00, 0x9e, 0x0f }) == "009e0f", "ToHexString"); Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.FromHexString("1af4b"), new byte[] { 0x01, 0xaf, 0x4b }), "FromHexString"); Assert.IsTrue(NetworkConverter.ToBoolean(new byte[] { 0x01 }), "ToBoolean"); Assert.IsTrue(NetworkConverter.ToChar(new byte[] { 0x00, 0x41 }) == 'A', "ToChar"); Assert.IsTrue(NetworkConverter.ToInt16(new byte[] { 0x74, 0xab }) == 0x74ab, "ToInt16"); Assert.IsTrue(NetworkConverter.ToUInt16(new byte[] { 0x74, 0xab }) == 0x74ab, "ToUInt16"); Assert.IsTrue(NetworkConverter.ToInt32(new byte[] { 0x74, 0xab, 0x05, 0xc1 }) == 0x74ab05c1, "ToInt32"); Assert.IsTrue(NetworkConverter.ToUInt32(new byte[] { 0x74, 0xab, 0x05, 0xc1 }) == 0x74ab05c1, "ToUInt32"); Assert.IsTrue(NetworkConverter.ToInt64(new byte[] { 0x74, 0xab, 0xa5, 0xbb, 0xf5, 0x39, 0x7b, 0x15 }) == 0x74aba5bbf5397b15, "ToInt64"); Assert.IsTrue(NetworkConverter.ToUInt64(new byte[] { 0x74, 0xab, 0xa5, 0xbb, 0xf5, 0x39, 0x7b, 0x15 }) == 0x74aba5bbf5397b15, "ToUInt64"); Assert.IsTrue(NetworkConverter.ToSingle(new byte[] { 0x4a, 0x8a, 0xd0, 0x64 }) == 4548658.0, "ToSingle"); Assert.IsTrue(NetworkConverter.ToDouble(new byte[] { 0x41, 0xb8, 0xa6, 0xb9, 0x83, 0x27, 0x97, 0xa3 }) == 413579651.15465754, "ToDouble"); Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes(true), new byte[] { 0x01 }), "GetBytes #bool"); Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((char)'A'), new byte[] { 0x00, 0x41 }), "GetBytes #char"); Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((short)0x74ab), new byte[] { 0x74, 0xab }), "GetBytes #short"); Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((ushort)0x74ab), new byte[] { 0x74, 0xab }), "GetBytes #ushort"); Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((int)0x74ab05c1), new byte[] { 0x74, 0xab, 0x05, 0xc1 }), "GetBytes #int"); Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((uint)0x74ab05c1), new byte[] { 0x74, 0xab, 0x05, 0xc1 }), "GetBytes #uint"); Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((long)0x74aba5bbf5397b15), new byte[] { 0x74, 0xab, 0xa5, 0xbb, 0xf5, 0x39, 0x7b, 0x15 }), "GetBytes #long"); Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((ulong)0x74aba5bbf5397b15), new byte[] { 0x74, 0xab, 0xa5, 0xbb, 0xf5, 0x39, 0x7b, 0x15 }), "GetBytes #ulong"); Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((float)4548658.0), new byte[] { 0x4a, 0x8a, 0xd0, 0x64 }), "GetBytes #float"); Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((double)413579651.15465754), new byte[] { 0x41, 0xb8, 0xa6, 0xb9, 0x83, 0x27, 0x97, 0xa3 }), "GetBytes #double"); Assert.IsTrue(NetworkConverter.ToInt32(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x74, 0xab, 0x05, 0xc1 }, 4) == 0x74ab05c1, "ToInt32"); for (int i = 0; i < 1024; i++) { byte[] buffer = new byte[_random.Next(0, 128)]; _random.NextBytes(buffer); var s = NetworkConverter.ToBase64UrlString(buffer); Assert.IsTrue(CollectionUtilities.Equals(buffer, NetworkConverter.FromBase64UrlString(s))); } for (int i = 0; i < 1024; i++) { byte[] buffer = new byte[_random.Next(0, 128)]; _random.NextBytes(buffer); var s = NetworkConverter.ToHexString(buffer); Assert.IsTrue(CollectionUtilities.Equals(buffer, NetworkConverter.FromHexString(s))); } }
public static byte[] GetHash(string item) { if (item == null) { throw new ArgumentNullException("item"); } try { lock (_hashCacheLockObject) { byte[] value; if (!_hashCache.TryGetValue(item, out value)) { var index = item.LastIndexOf('@'); if (index == -1) { return(null); } var nickname = item.Substring(0, index); var hash = item.Substring(index + 1); if (nickname.Length > 256) { return(null); } if (hash.Length > 256 || !Signature.CheckBase64(hash)) { return(null); } value = NetworkConverter.FromBase64UrlString(hash); _hashCache.Add(item, value); } return(value); } } catch (Exception) { return(null); } }
private void SendReceiveTestsRun() { var args = Environment.GetCommandLineArgs().Skip(1).ToArray(); if (args.Length == 2 && args[0] == "Upload") { Debug.Listeners.Add(new MyTraceListener((message) => { App.Current.Dispatcher.InvokeAsync(() => { this.Contents.Add(message); }); })); Debug.WriteLine(string.Join(" ", args)); Task.Run(() => { int port = int.Parse(args[1]); using (var simulationManager = new SimulationManager(port, (message) => Debug.WriteLine(message))) { simulationManager.Setup(); simulationManager.MessageUpload(); Thread.Sleep(1000 * 60 * 60 * 24); } App.Current.Shutdown(); }); } else { Debug.WriteLine("Download"); Task.Run(() => { var tuples = new LockedList <(Metadata metadata, Hash hash)>(); const int maxNodeCount = 40; var resetEvent = new ManualResetEvent(false); Task.Run(() => { Parallel.For(0, maxNodeCount, (i) => { var startInfo = new ProcessStartInfo(@"Amoeba.Simulation.exe"); startInfo.CreateNoWindow = true; startInfo.Arguments = $"Upload {i + 60000}"; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardInput = true; startInfo.UseShellExecute = false; using (var process = Process.Start(startInfo)) { process.WaitForInputIdle(); using (var metadataMemoryStream = new MemoryStream(NetworkConverter.FromBase64UrlString(process.StandardOutput.ReadLine()))) using (var hashMemoryStream = new MemoryStream(NetworkConverter.FromBase64UrlString(process.StandardOutput.ReadLine()))) { tuples.Add((Metadata.Import(metadataMemoryStream, BufferManager.Instance), Hash.Import(hashMemoryStream, BufferManager.Instance))); if (tuples.Count == maxNodeCount) { resetEvent.Set(); } } process.WaitForExit(); } });