Example #1
0
        public static string GetSignature(Certificate certificate)
        {
            if (certificate == null || certificate.Nickname == null || certificate.PublicKey == null)
            {
                return(null);
            }

            try
            {
                if (certificate.DigitalSignatureAlgorithm == DigitalSignatureAlgorithm.EcDsaP521_Sha256 ||
                    certificate.DigitalSignatureAlgorithm == DigitalSignatureAlgorithm.Rsa2048_Sha256)
                {
                    using (BufferStream bufferStream = new BufferStream(_bufferManager))
                    {
                        Signature.WriteString(bufferStream, certificate.Nickname);
                        bufferStream.Write(certificate.PublicKey, 0, certificate.PublicKey.Length);
                        bufferStream.Seek(0, SeekOrigin.Begin);

                        var signature = certificate.Nickname + "@" + NetworkConverter.ToBase64UrlString(Sha256.ComputeHash(bufferStream));
                        return(_signatureCache.GetValue(signature, certificate));
                    }
                }

                return(null);
            }
            catch (Exception)
            {
                return(null);
            }
        }
Example #2
0
 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
 {
     if (value is byte[] item)
     {
         return(NetworkConverter.ToBase64UrlString(item));
     }
     return("");
 }
Example #3
0
        private static string ToBase64String(Stream stream)
        {
            using (var targetStream = new RangeStream(stream, true))
                using (var safeBuffer = _bufferManager.CreateSafeBuffer((int)targetStream.Length))
                {
                    targetStream.Seek(0, SeekOrigin.Begin);
                    targetStream.Read(safeBuffer.Value, 0, (int)targetStream.Length);

                    return(NetworkConverter.ToBase64UrlString(safeBuffer.Value, 0, (int)targetStream.Length));
                }
        }
Example #4
0
        public void MessageUpload()
        {
            _callback.Invoke("----- CoreManager Message Upload Test (Start) -----");
            _callback.Invoke("");

            var random = RandomProvider.GetThreadRandom();

            _coreManager.Resize((long)1024 * 1024 * 1024 * 32).Wait();

            Metadata metadata = null;
            Hash     hash;

            using (var stream = new RecyclableMemoryStream(_bufferManager))
            {
                using (var safeBuffer = _bufferManager.CreateSafeBuffer(1024 * 4))
                {
                    for (long remain = (long)1024 * 1024 * 256; 0 < remain; remain -= safeBuffer.Value.Length)
                    {
                        int length = (int)Math.Min(remain, safeBuffer.Value.Length);

                        random.NextBytes(safeBuffer.Value);
                        stream.Write(safeBuffer.Value, 0, length);
                    }
                }

                stream.Seek(0, SeekOrigin.Begin);
                hash = new Hash(HashAlgorithm.Sha256, Sha256.Compute(new WrapperStream(stream)));

                stream.Seek(0, SeekOrigin.Begin);
                metadata = _coreManager.VolatileSetStream(stream, new TimeSpan(1, 0, 0, 0), CancellationToken.None).Result;
            }

            using (var stream = metadata.Export(_bufferManager))
                using (var safeBuffer = _bufferManager.CreateSafeBuffer((int)stream.Length))
                {
                    stream.Read(safeBuffer.Value, 0, (int)stream.Length);
                    Console.WriteLine(NetworkConverter.ToBase64UrlString(safeBuffer.Value, 0, (int)stream.Length));
                }

            using (var stream = hash.Export(_bufferManager))
                using (var safeBuffer = _bufferManager.CreateSafeBuffer((int)stream.Length))
                {
                    stream.Read(safeBuffer.Value, 0, (int)stream.Length);
                    Console.WriteLine(NetworkConverter.ToBase64UrlString(safeBuffer.Value, 0, (int)stream.Length));
                }

            _callback.Invoke("----- CoreManager Message Upload Test (End) -----");
        }
Example #5
0
        public static string ToTagString(Tag tag)
        {
            if (tag.Name == null || tag.Id == null)
            {
                return(null);
            }

            try
            {
                return(tag.Name + " - " + NetworkConverter.ToBase64UrlString(tag.Id));
            }
            catch (Exception e)
            {
                throw new ArgumentException("ArgumentException", e);
            }
        }
Example #6
0
        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)));
            }
        }
Example #7
0
        private static string ToBase64String(Stream stream)
        {
            byte[] buffer = null;

            try
            {
                buffer = _bufferManager.TakeBuffer((int)stream.Length);
                stream.Seek(0, SeekOrigin.Begin);
                stream.Read(buffer, 0, (int)stream.Length);

                return(NetworkConverter.ToBase64UrlString(buffer, 0, (int)stream.Length));
            }
            finally
            {
                if (buffer != null)
                {
                    _bufferManager.ReturnBuffer(buffer);
                }
            }
        }
Example #8
0
        public static string ToInfoMessage(Tag tag)
        {
            try
            {
                var builder = new StringBuilder();

                if (!string.IsNullOrWhiteSpace(tag.Name))
                {
                    builder.AppendLine(string.Format("{0}: {1}", LanguagesManager.Instance.Tag_Name, tag.Name));
                }
                if (tag.Id != null)
                {
                    builder.AppendLine(string.Format("{0}: {1}", LanguagesManager.Instance.Tag_Id, NetworkConverter.ToBase64UrlString(tag.Id)));
                }

                if (builder.Length != 0)
                {
                    return(builder.ToString().Remove(builder.Length - 2));
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception e)
            {
                throw new ArgumentException("ArgumentException", e);
            }
        }
Example #9
0
        private void MessageUploadAndDownload(IEnumerable <CoreManager> coreManagers)
        {
            _callback.Invoke("----- CoreManager Message Send and Receive Test -----");
            _callback.Invoke("");

            var coreManagerList = coreManagers.ToList();

            Parallel.ForEach(coreManagerList, coreManager =>
            {
                coreManager.Resize((long)1024 * 1024 * 1024 * 32).Wait();
            });

            var hashList     = new LockedHashSet <Hash>();
            var metadataList = new LockedList <Metadata>();

            Parallel.For(0, 30, new ParallelOptions()
            {
                MaxDegreeOfParallelism = 3
            }, i =>
            {
                var random = RandomProvider.GetThreadRandom();

                using (var stream = new BufferStream(_bufferManager))
                {
                    using (var safeBuffer = _bufferManager.CreateSafeBuffer(1024 * 4))
                    {
                        for (long remain = (long)1024 * 1024 * 32; remain > 0; remain = Math.Max(0, remain - safeBuffer.Value.Length))
                        {
                            int length = (int)Math.Min(remain, safeBuffer.Value.Length);

                            random.NextBytes(safeBuffer.Value);
                            stream.Write(safeBuffer.Value, 0, length);
                        }
                    }

                    stream.Seek(0, SeekOrigin.Begin);
                    hashList.Add(new Hash(HashAlgorithm.Sha256, Sha256.ComputeHash(new WrapperStream(stream, true))));

                    stream.Seek(0, SeekOrigin.Begin);
                    using (var tokenSource = new CancellationTokenSource())
                    {
                        metadataList.Add(coreManagerList[i].VolatileSetStream(stream, new TimeSpan(1, 0, 0), tokenSource.Token).Result);
                    }
                }
            });

            var sw = Stopwatch.StartNew();

            Parallel.ForEach(metadataList, metadata =>
            {
                for (; ;)
                {
                    Thread.Sleep(1000);

                    Stream stream = null;

                    try
                    {
                        stream = coreManagerList[0].VolatileGetStream(metadata, 1024 * 1024 * 256);
                        if (stream == null)
                        {
                            continue;
                        }

                        var hash = new Hash(HashAlgorithm.Sha256, Sha256.ComputeHash(stream));
                        if (!hashList.Contains(hash))
                        {
                            throw new ArgumentException("Broken");
                        }

                        _callback.Invoke($"{sw.Elapsed.ToString("hh\\:mm\\:ss")}: Success {NetworkConverter.ToBase64UrlString(metadata.Hash.Value)}");

                        return;
                    }
                    finally
                    {
                        if (stream != null)
                        {
                            stream.Dispose();
                        }
                    }
                }
            });

            _callback.Invoke("----- End -----");
        }