Пример #1
0
        /// <summary>
        /// ES: Cálculo de Nonce
        /// </summary>
        /// <param name="data">ES: Datos a labrar</param>
        /// <param name="config">ES: Configuración de labrado</param>
        /// <returns>ES: Resultado incluyendo Nonce y estadísticas</returns>
        public static FarmingResult Compute(byte[] data, FarmingConfig config)
        {
            var watch = Stopwatch.StartNew();

            var challenge  = config.Challenge ?? FarmingBytes.LessOrEqualThan;
            var hashRecipe = new HashRecipe(config.HashRecipe);

            byte[] hash;

            // Nonce initilization
            var nonceLength = config.NonceLength;

            byte[] nonce;
            if (config.NonceFromZero)
            {
                nonce = new byte[nonceLength];  // ES: inicia en 0
            }
            else
            {
                nonce = FarmingBytes.SeedNonce(nonceLength);   // ES: inicia en un valor aleatorio
            }
            // Nonce position
            int noncePosition;

            if (config.NoncePosition == FarmingNoncePosition.Head)
            {
                noncePosition = data.ChangeHead(nonce);
            }
            else
            {
                noncePosition = data.ChangeTail(nonce);
            }

            // Compute hash
            UInt64 hashCount = 0;

            do
            {
                data.IncNonce(noncePosition, nonceLength);  // TODO: handle when returns false (cycle)
                hash = hashRecipe.ComputeHash(data);
                hashCount++;
            }while (!challenge(hash, config.ChallengeValue));
            watch.Stop();

            nonce = data.ExtractNonce(noncePosition, nonceLength);

            return(new FarmingResult
            {
                Data = data,
                Nonce = nonce,
                Hash = hash,
                HashCount = hashCount,
                HashPerSecond = (hashCount * 1000.0F) / watch.ElapsedMilliseconds,
                FarmingTime = watch.ElapsedMilliseconds
            });
        }
Пример #2
0
        public void Compute_AsSha256d()
        {
            var data     = TestUtils.FromHex("416B5CDC9FE951BD361BD7ABFC120A5054758EBA88FDD68FD84E39D3B09AC25497D36B43CBE7B85A6A3CEBDA8DB4E5549C3EE51BB6FCB6AC1E");
            var sha256   = SHA256.Create();
            var expected = sha256.ComputeHash(sha256.ComputeHash(data));

            var recipe = new HashRecipe(new CryptoHashName[] { CryptoHashName.Sha256, CryptoHashName.Sha256 });
            var result = recipe.ComputeHash(data);

            Assert.IsTrue(TestUtils.AreEqual(expected, result));
        }
Пример #3
0
        public void Support_AddAlgorithm()
        {
            var data     = TestUtils.FromHex("416B5CDC9FE951BD361BD7ABFC120A5054758EBA88FDD68FD84E39D3B09AC25497D36B43CBE7B85A6A3CEBDA8DB4E5549C3EE51BB6FCB6AC1E");
            var step1    = SHA256.Create();
            var step2    = GroestlHashAlgorithm.Create(384);
            var step3    = SkeinHashAlgorithm.Create(224);
            var step4    = BlakeHashAlgorithm.Create(512);
            var step5    = SHA256.Create();
            var expected = step5.ComputeHash(step4.ComputeHash(step3.ComputeHash(step2.ComputeHash(step1.ComputeHash(data)))));

            var recipe = new HashRecipe();

            recipe.Add(CryptoHashName.Sha256);
            recipe.Add(CryptoHashName.Groestl384);
            recipe.Add(CryptoHashName.Skein224);
            recipe.Add(CryptoHashName.Blake512);
            recipe.Add(CryptoHashName.Sha256);
            var result = recipe.ComputeHash(data);

            Assert.IsTrue(TestUtils.AreEqual(expected, result));
        }
Пример #4
0
        /// <summary>
        /// ES: Comprobación del Nonce. Calcula el Hash con el Nonce indicado y comprueba que coincide
        /// con el Hash propuesto
        /// </summary>
        /// <param name="data">ES: Datos origen del Hash</param>
        /// <param name="nonce">ES: Nonce propuesto</param>
        /// <param name="config">ES: Configuración de labrado</param>
        /// <returns>ES: Resultados incluyendo el Nonce y Hash recalculados</returns>
        public static FarmingResult CheckNonce(byte[] data, byte[] nonce, FarmingConfig config)
        {
            var challenge   = config.Challenge ?? FarmingBytes.LessOrEqualThan;
            var hasherStack = new HashRecipe(config.HashRecipe);

            byte[] hash = hasherStack.ComputeHash(data);

            var result = new FarmingResult
            {
                Data      = data,
                Nonce     = nonce,
                Hash      = hash,
                HashCount = 0
            };

            if (challenge(hash, config.ChallengeValue))
            {
                int noncePosition;

                if (config.NoncePosition == FarmingNoncePosition.Head)
                {
                    noncePosition = 0;
                }
                else
                {
                    noncePosition = data.Length - config.NonceLength;
                }

                var dataNonce = data.ExtractNonce(noncePosition, config.NonceLength);
                result.Nonce = dataNonce;

                if (FarmingBytes.NoncesAreEqual(dataNonce, nonce))
                {
                    result.HashCount = 1;
                }
            }

            return(result);
        }