Beispiel #1
0
    def final(self, output=True):
        """Return hashed data as bytestring.

        `output` is primarily for internal use. It should only be False
        if you have a clear reason for doing so.

        This function can be called as either ``final`` or ``digest``.

        """
        self.tf.tweak[1] |= bigint(0x8000000000000000)  # SKEIN_T1_FLAG_FINAL
        buflen = len(self.buf)
        self.buf += zero_bytes[:64 - buflen]

        self._process_block(self.buf, buflen)

        if not output:
            hash_val = words2bytes(self.tf.key)
        else:
            hash_val = empty_bytes
            self.buf = zero_bytes[:]
            key = self.tf.key[:]  # temporary copy
            i = 0
            while i * 64 < self.digest_size:
                self.buf = words_format[1].pack(i) + self.buf[8:]
                self.tf.tweak = [0, self.block_type['out_final']]
                self._process_block(self.buf, 8)
                n = self.digest_size - i * 64
                if n >= 64:
                    n = 64
                hash_val += words2bytes(self.tf.key)[0:n]
                self.tf.key = key
                i += 1
        return hash_val
    def final(self, output=True):
        """Return hashed data as bytestring.

        `output` is primarily for internal use. It should only be False
        if you have a clear reason for doing so.

        This function can be called as either ``final`` or ``digest``.

        """
        self.tf.tweak[1] |= bigint(0x8000000000000000) # SKEIN_T1_FLAG_FINAL
        buflen = len(self.buf)
        self.buf += zero_bytes[:64-buflen]

        self._process_block(self.buf, buflen)

        if not output:
            hash_val = words2bytes(self.tf.key)
        else:
            hash_val = empty_bytes
            self.buf = zero_bytes[:]
            key = self.tf.key[:] # temporary copy
            i=0
            while i*64 < self.digest_size:
                self.buf = words_format[1].pack(i) + self.buf[8:]
                self.tf.tweak = [0, self.block_type['out_final']]
                self._process_block(self.buf, 8)
                n = self.digest_size - i*64
                if n >= 64:
                    n = 64
                hash_val += words2bytes(self.tf.key)[0:n]
                self.tf.key = key
                i+=1
        return hash_val
Beispiel #3
0
    def _process_block(self, block, byte_count_add):
        """Encrypt internal state using Threefish.

        Primarily for internal use.

        """
        block_len = len(block)
        for i in xrange(0, block_len, 64):
            w = bytes2words(block[i:i + 64])
            self.tf.tweak[0] = add64(self.tf.tweak[0], byte_count_add)
            self.tf.prepare_tweak()
            self.tf.prepare_key()
            self.tf.key = self.tf.encrypt_block(w)
            self.tf._feed_forward(self.tf.key, w)
            # set second tweak value to ~SKEIN_T1_FLAG_FIRST:
            self.tf.tweak[1] &= bigint(0xbfffffffffffffff)
    def _process_block(self, block, byte_count_add):
        """Encrypt internal state using Threefish.

        Primarily for internal use.

        """
        block_len = len(block)
        for i in xrange(0,block_len,64):
            w = bytes2words(block[i:i+64])
            self.tf.tweak[0] = add64(self.tf.tweak[0], byte_count_add)
            self.tf.prepare_tweak()
            self.tf.prepare_key()
            self.tf.key = self.tf.encrypt_block(w)
            self.tf._feed_forward(self.tf.key, w)
            # set second tweak value to ~SKEIN_T1_FLAG_FIRST:
            self.tf.tweak[1] &= bigint(0xbfffffffffffffff)