示例#1
0
        // Test encryption
        static void TestCryptography()
        {
            IHashProvider p;

            // Generate a random set of bytes
            byte[] Bytes = SecureRandom.Bytes(32);

            // Write bytes to console
            Console.WriteLine($"Randomly generated bytes: {Encoding.ByteArrayToHexString(Bytes)}");

            // Keccak the generated bytes
            byte[] KeccakBytes = Keccak.keccak(Bytes);

            // Write keccak bytes to console
            Console.WriteLine($"Keccak: {Encoding.ByteArrayToHexString(KeccakBytes)}");

            // Blake256 the generated bytes
            p = new Blake();
            byte[] BlakeBytes = p.Hash(Bytes);

            // Write blake256 bytes to console
            Console.WriteLine($"Blake: {Encoding.ByteArrayToHexString(BlakeBytes)}");

            // Skein the generated bytes
            p = new Skein();
            byte[] SkeinBytes = p.Hash(Bytes);

            // Write skein bytes to console
            Console.WriteLine($"Skein: {Encoding.ByteArrayToHexString(SkeinBytes)}");

            // Groestl the generated bytes
            p = new Groestl();
            byte[] GroestlBytes = p.Hash(Bytes);

            // Write groestl bytes to console
            Console.WriteLine($"Groestl: {Encoding.ByteArrayToHexString(GroestlBytes)}");

            // JH the generated bytes
            p = new JH();
            byte[] JHBytes = p.Hash(Bytes);

            // Write JH bytes to console
            Console.WriteLine($"JH: {Encoding.ByteArrayToHexString(JHBytes)}");

            // Base58 the generated bytes
            string Base58String = Base58.Encode(Bytes);

            // Write base58 bytes to console
            Console.WriteLine($"Base58: {Base58String}");
            Console.WriteLine();
        }
示例#2
0
        /* CryptoNight Step 5: Apply Keccak to the state again, and then
         * use the resulting data to select which of four finalizer
         * hash functions to apply to the data (Blake, Groestl, JH,
         * or Skein). Use this hash to squeeze the state array down
         * to the final 32 byte hash output.
         */
        public static byte[] HashFinalState(CNState cnState)
        {
            /* Get the state buffer as an array of ulongs rather than bytes */
            ulong[] hashState = cnState.GetHashState();

            Keccak.Keccakf(hashState, 24);

            /* Set the state buffer from the coerced hash state */
            cnState.SetHashState(hashState);

            /* Get the actual state buffer finally */
            byte[] state = cnState.GetState();

            /* Choose the final hashing function to use based on the value of
             * state[0] */
            switch (state[0] % 4)
            {
            case 0:
            {
                return(Blake.Hash(state));
            }

            case 1:
            {
                return(Groestl.Hash(state));
            }

            case 2:
            {
                return(JH.Hash(state));
            }

            default:
            {
                return(Skein.Hash(state));
            }
            }
        }