コード例 #1
0
ファイル: Sponge.cs プロジェクト: xgalv/Cryptool2
        public Sponge(int rate, int capacity, ref KeccakPres pres, Keccak plugin, int progressionSteps)
        {
            Debug.Assert(rate > 0);
            Debug.Assert(widthOfPermutation.Contains(rate + capacity));

            this.rate     = rate;
            this.capacity = capacity;
            laneSize      = (rate + capacity) / 25;

            keccak_f = new Keccak_f(capacity + rate, ref state, ref pres, plugin);

            state                 = new byte[capacity + rate];
            this.pres             = pres;
            this.plugin           = plugin;
            this.progressionSteps = progressionSteps;
        }
コード例 #2
0
        public static byte[] Hash(byte[] input, int outputLength, int rate, int capacity, ref KeccakPres pres, Keccak plugin)
        {
            #if _DEBUG_
            Console.WriteLine("#Keccak: running Keccak with the following parameters:");
            Console.WriteLine(
                "#Keccak: output length\t{0} bits\n" +
                "#Keccak: state size\t{1} bits\n" +
                "#Keccak: bit rate\t\t{2} bits\n" +
                "#Keccak: capacity\t\t{3} bits\n\n"
                , outputLength, rate + capacity, rate, capacity);
            #endif

            /* map each bit of the input to a byte */
            byte[] inputInBits = ByteArrayToBitArray(input);

            /* for presentation: estimate number of keccak-f executions */
            int progressionSteps = (int)Math.Ceiling((double)(inputInBits.Length + 8) / rate) + ((int)Math.Ceiling((double)outputLength / rate) - 1);

            /* create sponge instance */
            Sponge sponge = new Sponge(rate, capacity, ref pres, plugin, progressionSteps);

            /* absorb input */
            sponge.Absorb(inputInBits);

            /* squeeze sponge to obtain output */
            Debug.Assert(outputLength % 8 == 0);
            byte[] outputInBits = sponge.Squeeze(outputLength);

            /* reverse 'bit to byte' mapping */
            byte[] output = BitArrayToByteArray(outputInBits);

            #if _DEBUG_
            Console.WriteLine("#Keccak: successfully hashed {0} input bits to {1} output bits!", inputInBits.Length, outputInBits.Length);
            Console.WriteLine("#Keccak: all work is done!");
            #endif


            return(output);
        }