示例#1
0
def sponge_function(hash_input, key='', output_size=32, capacity=32, rate=32, 
                    mixing_subroutine=None,
                    mode_of_operation=variable_length_hash,
                    absorb_mode=xor_subroutine): 
    assert mixing_subroutine is not None
    #print "Hashing: ", [byte for byte in hash_input]
    if isinstance(rate, int):
        rate = slice(0, rate)
    if isinstance(capacity, int):
        end_of_rate = rate.stop
        capacity = slice(end_of_rate, end_of_rate + capacity)
    state_size = capacity.stop
    state = bytearray(state_size)
    if key:
        absorb(key, state, rate, mixing_subroutine, absorb_mode)      
    
    rate_in_bytes = rate.stop - rate.start
    hash_input = pad_input(bytearray(hash_input), rate_in_bytes)
        
    #print "\nBeginning absorption", [byte for byte in hash_input]
    for _bytes in slide(hash_input, rate_in_bytes):
        #print "Absorbing: ", [byte for byte in _bytes]
        absorb(_bytes, state, rate, mixing_subroutine, absorb_mode)
    
 #   print "\nBeginning final mix before mode of operation: ", [byte for byte in state]
    mixing_subroutine(state)
#    print "State: ", [byte for byte in state]
    return mode_of_operation(state, rate, output_size, mixing_subroutine, absorb_mode)                
示例#2
0
def sponge_function(hash_input,
                    key='',
                    output_size=32,
                    capacity=32,
                    rate=32,
                    mixing_subroutine=None,
                    mode_of_operation=variable_length_hash,
                    absorb_mode=xor_subroutine):
    assert mixing_subroutine is not None
    #print "Hashing: ", [byte for byte in hash_input]
    if isinstance(rate, int):
        rate = slice(0, rate)
    if isinstance(capacity, int):
        end_of_rate = rate.stop
        capacity = slice(end_of_rate, end_of_rate + capacity)
    state_size = capacity.stop
    state = bytearray(state_size)
    if key:
        absorb(key, state, rate, mixing_subroutine, absorb_mode)

    rate_in_bytes = rate.stop - rate.start
    hash_input = pad_input(bytearray(hash_input), rate_in_bytes)

    #print "\nBeginning absorption", [byte for byte in hash_input]
    for _bytes in slide(hash_input, rate_in_bytes):
        #print "Absorbing: ", [byte for byte in _bytes]
        absorb(_bytes, state, rate, mixing_subroutine, absorb_mode)

#   print "\nBeginning final mix before mode of operation: ", [byte for byte in state]
    mixing_subroutine(state)
    #    print "State: ", [byte for byte in state]
    return mode_of_operation(state, rate, output_size, mixing_subroutine,
                             absorb_mode)
示例#3
0
def strange_hash(data, key=KEY): 
    output = list()
    state = 0
    data = bytes_to_words(bytearray(pad_input(data, 2)), 2)
    for index, word in enumerate(data):        
        state ^= key[branch(word ^ index)]        
        output.append(state)        
    return odd_size_to_bytes(output, 12)
示例#4
0
def memory_hard_hash(data, rounds, hash_key=HASH_KEY, modulus=WORD_COUNT):     
    output = [0 for count in range(STATE_SIZE)]    
    hash_input = bytes_to_words(bytearray(pad_input(data, STATE_SIZE * WORD_BYTES)), WORD_BYTES)    
    for round in range(rounds):
        for index, word in enumerate(hash_input):            
            value = hash_key[(word + round + output[index % STATE_SIZE]) % modulus]
            addition_subroutine(output, value, modulus)              
    return bytes(words_to_bytes(output, WORD_BYTES))
示例#5
0
def hamming_weight_hash(data, output_size=32):
    accumulator = 0    
    data = bytearray(pad_input(data[:], 32))    
    output = bytearray(output_size)
    for counter, word in enumerate(slide(data, 4)):
        word_weight = hamming_weight(cast(word, "integer"))
        accumulator += word_weight + choice(accumulator, word_weight, counter) + counter            
        output[counter] = (output[counter] + accumulator)
    return output
示例#6
0
def memory_hard_hash(data, rounds, hash_key=HASH_KEY, modulus=WORD_COUNT):
    output = [0 for count in range(STATE_SIZE)]
    hash_input = bytes_to_words(
        bytearray(pad_input(data, STATE_SIZE * WORD_BYTES)), WORD_BYTES)
    for round in range(rounds):
        for index, word in enumerate(hash_input):
            value = hash_key[(word + round + output[index % STATE_SIZE]) %
                             modulus]
            addition_subroutine(output, value, modulus)
    return bytes(words_to_bytes(output, WORD_BYTES))
示例#7
0
def permute_hash(data, rounds=1, blocksize=16):
    data = list(bytearray(pad_input(data, blocksize)))    
    output = [0 for byte in range(blocksize)]
    key = data[:blocksize]
    for round in range(rounds):
        for data_block in slide(data, blocksize):                
            permutation(data_block, key)
            key = data_block           
            
    return bytes(bytearray((byte >> 8) ^ (byte & 255) for byte in data_block))
示例#8
0
def hamming_weight_hash(data, output_size=32):
    accumulator = 0
    data = bytearray(pad_input(data[:], 32))
    output = bytearray(output_size)
    for counter, word in enumerate(slide(data, 4)):
        word_weight = hamming_weight(cast(word, "integer"))
        accumulator += word_weight + choice(accumulator, word_weight,
                                            counter) + counter
        output[counter] = (output[counter] + accumulator)
    return output
示例#9
0
def permute_hash(data, rounds=1, blocksize=16):
    data = list(bytearray(pad_input(data, blocksize)))
    output = [0 for byte in range(blocksize)]
    key = data[:blocksize]
    for round in range(rounds):
        for data_block in slide(data, blocksize):
            permutation(data_block, key)
            key = data_block

    return bytes(bytearray((byte >> 8) ^ (byte & 255) for byte in data_block))
示例#10
0
def strange_hash(data): # outputs span a 5-bit range; the hash of the first 32 integers yields the 32 outputs the hash can produce; 
    output = bytearray()
    state = 0
    key = list(set([branch(word) for word in range(256)]))
    key = dict((entry, key.index(entry)) for entry in key)

    data = bytearray(pad_input(data, 1))
    for index, byte in enumerate(data):        
        state ^= key[branch(byte ^ index)]
        output.append(state)        
    return quints_to_bytes(output)
示例#11
0
def hash_function(data):
    data = bytes_to_words(pad_input(bytearray(data), 64), 4)
    return bytes(words_to_bytes(compression_function(data), 4))
示例#12
0
文件: hash.py 项目: overgter/crypto
def hash_function(data):
    data = pad_input(bytearray(data), 32)
    return compression_function(data)
示例#13
0
def hash_function(message):
    message = pad_input(message, 32)
    return compression_function(message)
        
示例#14
0
def hash_function(data):
    data = bytes_to_words(pad_input(bytearray(data), 64), 4)
    return bytes(words_to_bytes(compression_function(data), 4))