ComputeHash() public static method

public static ComputeHash ( ArraySegment value ) : byte[]
value ArraySegment
return byte[]
Ejemplo n.º 1
0
            public int Verify_1(byte[] key, byte[] value)
            {
                if (key == null)
                {
                    throw new ArgumentNullException("key");
                }
                if (key.Length != 32)
                {
                    throw new ArgumentOutOfRangeException("key");
                }
                if (value == null)
                {
                    throw new ArgumentNullException("value");
                }
                if (value.Length != 32)
                {
                    throw new ArgumentOutOfRangeException("value");
                }

                var bufferManager = BufferManager.Instance;

                try
                {
                    byte[] result;

                    {
                        byte[] buffer = bufferManager.TakeBuffer(64);
                        Unsafe.Copy(key, 0, buffer, 0, 32);
                        Unsafe.Copy(value, 0, buffer, 32, 32);

                        result = Sha256.ComputeHash(buffer, 0, 64);

                        bufferManager.ReturnBuffer(buffer);
                    }

                    int count = 0;

                    for (int i = 0; i < 32; i++)
                    {
                        for (int j = 0; j < 8; j++)
                        {
                            if (((result[i] << j) & 0x80) == 0)
                            {
                                count++;
                            }
                            else
                            {
                                goto End;
                            }
                        }
                    }
End:

                    return(count);
                }
                catch (Exception)
                {
                    return(0);
                }
            }
Ejemplo n.º 2
0
        public Cash Create(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (this == null || this.Limit == 0 || this.ComputationTime <= TimeSpan.Zero)
            {
                return(null);
            }

            if (this.CashAlgorithm == CashAlgorithm.Version1)
            {
                _isCanceled = false;

                var minerUtilities = new MinerUtilities();

                try
                {
                    var task = Task.Factory.StartNew(() =>
                    {
                        var key = minerUtilities.Create_1(Sha256.ComputeHash(stream), this.Limit, this.ComputationTime);
                        return(new Cash(CashAlgorithm.Version1, key));
                    });

                    while (!task.IsCompleted)
                    {
                        if (_isCanceled)
                        {
                            minerUtilities.Cancel();
                        }

                        Thread.Sleep(1000);
                    }

                    return(task.Result);
                }
                catch (AggregateException e)
                {
                    throw e.InnerExceptions.FirstOrDefault();
                }
            }

            return(null);
        }
Ejemplo n.º 3
0
        public static int Verify(Cash cash, Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (cash == null)
            {
                return(0);
            }

            if (cash.CashAlgorithm == CashAlgorithm.Version1)
            {
                var minerUtilities = new MinerUtilities();

                return(minerUtilities.Verify_1(cash.Key, Sha256.ComputeHash(stream)));
            }

            return(0);
        }
Ejemplo n.º 4
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);
            }
        }