예제 #1
0
        public byte[] ComputeHash(IHashFactory hashFactory)
        {
            IByteConverter converter = hashFactory.GetByteConverter();
            string         rawData   = converter.ConvertToString(Nonce) + converter.ConvertToString(Sender) + converter.ConvertToString(Recipient) + Amount;

            byte[] data = Encoding.ASCII.GetBytes(rawData);

            return(hashFactory.GetDigest().GetHash(data));
        }
        public string FromBase64Bytes(IReadOnlyCollection <byte> input)
        {
            if (input == null)
            {
                return(null);
            }

            var inputString = ByteConverter.ConvertToString(input);
            var resultBytes = FromBase64String(inputString);
            var result      = ByteConverter.ConvertToString(resultBytes);

            return(result);
        }
예제 #3
0
        public byte[] ComputeHash(IHashFactory hashFactory)
        {
            IByteConverter converter = hashFactory.GetByteConverter();
            IDigest        digest    = hashFactory.GetDigest();

            string data = converter.ConvertToString(Nonce) +
                          converter.ConvertToString(PreviousHash) +
                          CreationTime.ToString() +
                          converter.ConvertToString(MerkleRoot) +
                          converter.ConvertToString(MinerAddress) +
                          HashAlgorithmId +
                          Difficulty;

            byte[] rawData = Encoding.ASCII.GetBytes(data);

            return(digest.GetHash(rawData));
        }
예제 #4
0
                public string Decrypt(string cipherText)
                {
                    if (cipherText == null)
                    {
                        throw new ArgumentNullException("cipherText");
                    }

                    var hmacSaltBytes   = ConvertToUTF8Bytes(ByteConverter, HmacSaltValue);
                    var cryptKeyBytes   = ConvertToUTF8Bytes(ByteConverter, CryptKeyValue);
                    var cipherTextBytes = ConvertFromBase64String(Base64Converter, cipherText);
                    var clearBytes      = Decrypt(
                        keyBitSize: KeyBitSize,
                        blockBitSize: BlockBitSize,
                        encryptedMessage: cipherTextBytes,
                        cryptKey: cryptKeyBytes,
                        authKey: hmacSaltBytes
                        );

                    var result = ByteConverter.ConvertToString(clearBytes);

                    return(result);
                }
        public void When_ToBase64Bytes_GivenEmpty_ExpectEmpty()
        {
            // arrange
            const string input    = "";
            const string expected = "";

            // act
            var actualBytes = _Base64Converter.ToBase64Bytes(input);
            var actual      = _UTF8Converter.ConvertToString(actualBytes);

            // assert
            Assert.IsNotNull(actual);
            Assert.AreEqual(expected: expected, actual: actual);
        }
예제 #6
0
        public void MineBlock(Block block)
        {
            INonceGenerator nonceGenerator = HashFactory.GetNonceGenerator();
            IByteConverter  converter      = HashFactory.GetByteConverter();

            if (Difficulty >= HashFactory.GetDigest().HashLength)
            {
                throw new ArgumentException("Difficulty can not be greater or equal to hash length.");
            }

            if (Difficulty <= 0)
            {
                throw new ArgumentException("Difficulty can not be greater or equal to zero.");
            }

            byte[] target = new byte[Difficulty];

            block.HashAlgorithmId = HashAlgorithmId;
            block.Difficulty      = Difficulty;

            nonceGenerator.Reset();

            #region Parallel mining.

            CancellationTokenSource tokenSource = new CancellationTokenSource();
            object locker        = new object();
            byte[] solutionNonce = null;
            byte[] solutionHash  = null;

            string immutableData = converter.ConvertToString(block.PreviousHash) +
                                   block.CreationTime.ToString() +
                                   converter.ConvertToString(block.MerkleRoot) +
                                   converter.ConvertToString(block.MinerAddress) +
                                   block.HashAlgorithmId +
                                   block.Difficulty;
            try
            {
                Parallel.ForEach(nonceGenerator, new ParallelOptions()
                {
                    CancellationToken = tokenSource.Token
                }, (byte[] nonce) =>
                {
                    IDigest digest = HashFactory.GetDigest();

                    converter = HashFactory.GetByteConverter();

                    string dataWithNonce = converter.ConvertToString(nonce) + immutableData;
                    byte[] rawData       = Encoding.ASCII.GetBytes(dataWithNonce);
                    byte[] hash          = digest.GetHash(rawData);

                    if (hash.Take(Difficulty).SequenceEqual(target))
                    {
                        lock (locker)
                        {
                            solutionNonce = nonce;
                            solutionHash  = hash;
                        }

                        tokenSource.Cancel();
                    }
                });
            }
            catch (OperationCanceledException)
            {
            }

            block.Nonce = solutionNonce ?? throw new MiningFailureException("Couldn't resolve block", block);
            block.Hash  = solutionHash;

            #endregion
        }