Python 如何修复错误类型错误:需要类似字节的对象,而不是';str';?

Python 如何修复错误类型错误:需要类似字节的对象,而不是';str';?,python,python-3.x,Python,Python 3.x,这是我尝试运行脚本时出现的错误 #-*- coding: utf-8 -*- import sha3 import pyopencl as cl import Keccak import time #Initialize OpenCL platforms = cl.get_platforms() devices = platforms[0].get_devices() context = cl.Context(devices[:2]) queue = cl.CommandQueue(contex

这是我尝试运行脚本时出现的错误

#-*- coding: utf-8 -*-
import sha3
import pyopencl as cl
import Keccak
import time
#Initialize OpenCL
platforms = cl.get_platforms()
devices = platforms[0].get_devices()
context = cl.Context(devices[:2])
queue = cl.CommandQueue(context, context.devices[0],
                        properties=cl.command_queue_properties.PROFILING_ENABLE)
program = cl.Program(context, open('sha3.cl').read()).build(options='')

#Parameters for SHA 512
r = 576
c = 1024
n = 512

inputlist = []
inputlist.append("a" * 1000)


start = time.time()
result = sha3.Keccak(inputlist, n, r,c, program, context, queue)
print  ("Hashing Result is")
print (result)
print ("Time taken is: " + str(time.time() - start))
回溯(最近一次呼叫最后一次):
文件“J:/puzzutils/AllInOneWithPermutation/eeeeee.py”,第24行,in
result=sha3.Keccak(输入列表、n、r、c、程序、上下文、队列)
文件“J:/puzzutils/AllInOneWithPermutation\sha3.py”,第195行,Keccak格式
gpu_string=cl.Buffer(上下文,cl.mem_flags.READ_ONLY | cl.mem_flags.COPY_HOST_PTR,hostbuf=HOST_string)
TypeError:需要类似字节的对象,而不是“str”
不管我做什么。我找不到导致此错误的原因,当我尝试添加encode()时,它的给定列表没有属性“encode”

我正在尝试重新编写这个opencl keccak实现脚本 为了让它在python 3.8上工作

Traceback (most recent call last):
  File "J:/PuzzleUtils/AllInOneWithPermutation/eeeee.py", line 24, in <module>
    result = sha3.Keccak(inputlist, n, r,c, program, context, queue)
  File "J:/PuzzleUtils/AllInOneWithPermutation\sha3.py", line 195, in Keccak
    gpu_string = cl.Buffer(context, cl.mem_flags.READ_ONLY | cl.mem_flags.COPY_HOST_PTR, hostbuf=host_string)
TypeError: a bytes-like object is required, not 'str'
来自未来进口部的

导入系统
将pyopencl作为cl导入
将numpy作为np导入
进口派拉布
导入pdb
导入时间
def pad10star1(米,n):
“”“使用pad10*1填充规则填充M,以达到r位的长度倍数
M:消息对(长度以位为单位,十六进制字符字符串('9AFC…'))
n:以位为单位的长度(必须是8的倍数)
示例:pad10star1([60,'BA594E0FB9EBBD30',8)返回'BA594E0FB9EBBD93'
"""
[我的字符串长度,我的字符串]=M
#检查参数n
如果n%8!=0:
raise KECCAKEROR.KECCAKEROR(“n必须是8的倍数”)
#检查所提供字符串的长度
如果len(my_字符串)%2!=0:
#用一个“0”填充以达到正确长度(不知道测试
#矢量编码)
我的字符串=我的字符串+'0'
如果my_string_length>(len(my_string)//2*8):
raise KECCAKEROR.KECCAKEROR(“字符串太短,无法包含已宣布的位数”)
nr_bytes_filled=my_string_length//8
nbr\u位\u填充=我的字符串\u长度%8
l=我的字符串长度%n
如果((n-8)(8-nbr_位填充))
my_byte=my_byte+2**(nbr_位填充)+2**7
my_byte=“%02X”%my_byte
my_字符串=my_字符串[0:nr_字节\u填充*2]+my_字节
其他:
如果(nbr\u位\u填充==0):
我的字节=0
其他:
my_byte=int(my_string[nr_bytes_filled*2:nr_bytes_filled*2+2],16)
my_字节=(my_字节>>(8-nbr_位填充))
my_byte=my_byte+2**(nbr_位已填充)
my_byte=“%02X”%my_byte
my_字符串=my_字符串[0:nr_字节\u填充*2]+my_字节
而((8*len(my_string)//2)%n<(n-8)):
我的字符串=我的字符串+'00'
我的字符串=我的字符串+'80'
返回我的字符串
def KeccakF(到散列、迭代、当前、程序、上下文、队列):
字长=64
inputnum=int(到\u hash.shape[0]/5)
#设置舍入常量
RC=[0x0000000000000001,
0x0000000000008082,
0x8000000000808A,
0x8000000080008000,
0x000000000000808B,
0x0000000080000001,
0x8000000080008081,
0x80000000008009,
0x000000000000008A,
0x0000000000000088,
0x0000000080008009,
0x000000008000000A,
0x000000008000808B,
0x8000000000008B,
0x80000000008089,
0x80000000008003,
0x80000000008002,
0x80000000000080,
0x000000000000800A,
0x80000000A,
0x8000000080008081,
0x80000000008080,
0x0000000080000001,
0x8000000080008008]
round_常量=np.array([np.uint64(x)表示RC中的x])
round_常量_gpu=cl.Buffer(上下文,cl.mem_flags.READ_ONLY,8*len(round_常量))#为什么*8???
cl.enqueue\U copy(队列,循环常数\U gpu,循环常数,is\U blocking=False)
#设置旋转偏移
旋转偏移=np.数组([0,36,3,41,18\
1 ,44 ,10 ,45 ,2,   \
62, 6 ,43 ,15 ,61,  \
28, 55, 25, 21, 56, \
27, 20, 39, 8 ,14])
旋转偏移=np.数组([np.uint64(x)表示旋转偏移中的x])
旋转\u gpu\u buffer=cl.buffer(上下文,cl.mem\u flags.READ\u,8*len(旋转偏移))
cl.enqueue\U复制(队列、旋转\U gpu\U缓冲区、旋转\U偏移,is\U阻塞=假)
stuff_to_hash=cl.Buffer(上下文,cl.mem_flags.READ_ONLY,to_hash.size*8)
cl.enqueue_copy(队列,填充到散列,到散列,is_blocking=False)#is_block=True表示等待完成
#用于GPU写入最终哈希的缓冲区
gpu_final_hash=cl.Buffer(上下文,cl.mem_flags.READ_WRITE,to_hash.size*8)
#控制Keccak中每个哈希的迭代次数
gpu_迭代=cl.Buffer(上下文,cl.mem_flags.READ_ONLY,len(迭代)*8)
cl.enqueue\u copy(队列,gpu迭代,np.array(迭代),is_blocking=False)#is_block=True表示等待完成
gpu curr\u iter=cl.Buffer(上下文,cl.mem\u flags.READ\u ONLY,8)
cl.enqueue\u copy(队列、gpu\u curr\u iter、np.array(curr\u iter)、is\u block=False)\is\u block=True表示等待完成
#创建5x5工作组,本地缓冲区
本地_大小,全局_大小=(5,5),(5,5*inputnum)
局部的,局部的=np.uint64(5),np.uint64(5)
A=cl.LocalMemory(8*25)
B=cl.LocalMemory(8*25)
C=cl.LocalMemory(8*25)
D=cl.LocalMemory(8*25)
#散列输入
final_hash=np.zero((5*inputnum,5))
final\u hash=np.array([np.uint64(x)表示final\u hash中的x])
hash_event=program.sha_3_hash(队列、全局大小、本地大小、,
填充到散列、gpu最终散列、旋转gpu缓冲区、四舍五入常量gpu、gpu迭代、gpu电流、,
B、 A、C、D、本地(本地、本地)
cl.enqueue\u copy(队列、最终\u散列、gpu最终\u散列,is\u blocking=True)
返回最终散列
def Keccak(输入列表、n、r、c、程序、上下文、队列):
inputnum=len(inputlist)
input\u str=inputlist[0]
#P是填充输入的存储器
P=[]
#Z是输出散列的存储器
Z=[]
迭代次数=[]
#开始=时间。时间()
###填充阶段
对于范围内的i(inputnum):
tmpstr=pad10
from __future__ import division
import sys
import pyopencl as cl
import numpy as np
import pylab
import pdb
import time

def pad10star1(M, n):
    """Pad M with the pad10*1 padding rule to reach a length multiple of r bits
    M: message pair (length in bits, string of hex characters ('9AFC...')
    n: length in bits (must be a multiple of 8)
    Example: pad10star1([60, 'BA594E0FB9EBBD30'],8) returns 'BA594E0FB9EBBD93'
    """

    [my_string_length, my_string]=M

    # Check the parameter n
    if n%8!=0:
        raise KeccakError.KeccakError("n must be a multiple of 8")

    # Check the length of the provided string
    if len(my_string)%2!=0:
        #Pad with one '0' to reach correct length (don't know test
        #vectors coding)
        my_string=my_string+'0'
    if my_string_length>(len(my_string)//2*8):
        raise KeccakError.KeccakError("the string is too short to contain the number of bits announced")

    nr_bytes_filled=my_string_length//8
    nbr_bits_filled=my_string_length%8
    l = my_string_length % n
    if ((n-8) <= l <= (n-2)):
        if (nbr_bits_filled == 0):
            my_byte = 0
        else:
            my_byte=int(my_string[nr_bytes_filled*2:nr_bytes_filled*2+2],16)
        my_byte=(my_byte>>(8-nbr_bits_filled))
        my_byte=my_byte+2**(nbr_bits_filled)+2**7
        my_byte="%02X" % my_byte
        my_string=my_string[0:nr_bytes_filled*2]+my_byte
    else:
        if (nbr_bits_filled == 0):
            my_byte = 0
        else:
            my_byte=int(my_string[nr_bytes_filled*2:nr_bytes_filled*2+2],16)
        my_byte=(my_byte>>(8-nbr_bits_filled))
        my_byte=my_byte+2**(nbr_bits_filled)
        my_byte="%02X" % my_byte
        my_string=my_string[0:nr_bytes_filled*2]+my_byte
        while((8*len(my_string)//2)%n < (n-8)):
            my_string=my_string+'00'
        my_string = my_string+'80'

    return my_string

def KeccakF(to_hash, iterations, curr_iter, program, context, queue):



    WORDLENGTH = 64
    inputnum = int(to_hash.shape[0]/5)
    #Set up Round constants
    RC=[0x0000000000000001,
        0x0000000000008082,
        0x800000000000808A,
        0x8000000080008000,
        0x000000000000808B,
        0x0000000080000001,
        0x8000000080008081,
        0x8000000000008009,
        0x000000000000008A,
        0x0000000000000088,
        0x0000000080008009,
        0x000000008000000A,
        0x000000008000808B,
        0x800000000000008B,
        0x8000000000008089,
        0x8000000000008003,
        0x8000000000008002,
        0x8000000000000080,
        0x000000000000800A,
        0x800000008000000A,
        0x8000000080008081,
        0x8000000000008080,
        0x0000000080000001,
        0x8000000080008008]

    round_constants = np.array([np.uint64(x) for x in RC])
    round_constants_gpu = cl.Buffer(context, cl.mem_flags.READ_ONLY, 8 * len(round_constants))   #Why * 8???
    cl.enqueue_copy(queue, round_constants_gpu, round_constants, is_blocking=False)

    #Set up rotation offsets
    rotation_offsets = np.array([0, 36, 3, 41, 18, \
                                1 ,44 ,10 ,45 ,2,   \
                                62, 6 ,43 ,15 ,61,  \
                                28, 55, 25, 21, 56, \
                                27, 20, 39, 8 ,14])

    rotation_offsets = np.array([np.uint64(x) for x in rotation_offsets])
    rotation_gpu_buffer = cl.Buffer(context, cl.mem_flags.READ_ONLY, 8 * len(rotation_offsets))
    cl.enqueue_copy(queue, rotation_gpu_buffer, rotation_offsets, is_blocking=False)




    stuff_to_hash = cl.Buffer(context, cl.mem_flags.READ_ONLY, to_hash.size * 8)
    cl.enqueue_copy(queue, stuff_to_hash, to_hash, is_blocking=False)#is_block=True means wait for completion

    #Buffer for GPU to write final hash
    gpu_final_hash = cl.Buffer(context, cl.mem_flags.READ_WRITE, to_hash.size * 8)
    
    #control the number of iterations of each hash in Keccak
    gpu_iterations = cl.Buffer(context, cl.mem_flags.READ_ONLY, len(iterations)*8)
    cl.enqueue_copy(queue, gpu_iterations, np.array(iterations), is_blocking=False)#is_block=True means wait for completion

    gpu_curr_iter = cl.Buffer(context, cl.mem_flags.READ_ONLY, 8)
    cl.enqueue_copy(queue, gpu_curr_iter, np.array(curr_iter), is_blocking=False)#is_block=True means wait for completion


    #Create 5x5 workgroup, local buffer
    local_size, global_size = (5, 5) , (5,5*inputnum)
    local_buf_w,local_buf_h = np.uint64(5),np.uint64(5)
    A = cl.LocalMemory(8*25)
    B = cl.LocalMemory(8*25)
    C = cl.LocalMemory(8*25)
    D = cl.LocalMemory(8*25)

    #Hash input
    final_hash = np.zeros((5*inputnum,5))
    final_hash = np.array([np.uint64(x) for x in final_hash])    
    hash_event = program.sha_3_hash(queue, global_size, local_size,
                              stuff_to_hash, gpu_final_hash,rotation_gpu_buffer,round_constants_gpu, gpu_iterations, gpu_curr_iter,
                              B,A, C, D, local_buf_w,local_buf_h)

    
    
    cl.enqueue_copy(queue, final_hash, gpu_final_hash, is_blocking=True)

    return final_hash


def Keccak(inputlist, n,r,c, program, context, queue):



    inputnum = len(inputlist)
    input_str = inputlist[0]

    #P is a storage for the padded inputs
    P = []

    #Z is a storage for the output hashes
    Z = []

    iterations = []

    #start = time.time()
    ### Padding Phase
    for i in range(inputnum):
        tmpstr = pad10star1([len(inputlist[i])*4, inputlist[i]],r) 
        P.append(tmpstr)
        Z.append("")
        iterations.append((len(tmpstr)*8//2)//r)

    #print ("Time to run padding: " + str(time.time() - start))

    # Initialisation of state
    S = np.zeros((5*inputnum,5))


    #Testing
    S = np.array([np.uint64(x) for x in S])

    #Initialize workgroup sizes for the gpu
    local_size, global_size = (5, 5) , (5,5*inputnum)

    for i in range(max(iterations)): 

        host_string = ""
        Pi = np.zeros((5*inputnum,5))
        Pi = np.array([np.uint64(x) for x in Pi])

        for j in range(inputnum):

            if (iterations[j] > i):
                #Absorbing Phase
                host_string = host_string + str(P[j][i*(2*r//8):(i+1)*(2*r//8)]+'00'*(c//8))
            else:
                #Dummy variables. Won't be used.
                host_string = host_string + "0"*400

        gpu_string = cl.Buffer(context, cl.mem_flags.READ_ONLY | cl.mem_flags.COPY_HOST_PTR, hostbuf=host_string)
        gpu_table = cl.Buffer(context, cl.mem_flags.READ_WRITE, 25*8 * inputnum)
        part_of_string = cl.LocalMemory(1*16)
        program.convert_str_to_table(queue,global_size,local_size, gpu_string, gpu_table, part_of_string, np.uint64(5),np.uint64(5),np.uint64(64))
        cl.enqueue_copy(queue, Pi, gpu_table, is_blocking=True)




        for x in range(5*inputnum):
            for y in range(5):
                #print 'type S:',type(S[x][y]),'type P:',type(Pi[x][y])

                if (iterations[int(x/5)] > i):
                    S[x][y] = S[x][y]^Pi[x][y]



        S = np.array([np.uint64(x) for x in S])
        #start = time.time()
        S = KeccakF(S, iterations, i,  program, context, queue)
        #print ("Time to run KeccakF: " + str(time.time() - start))
        #print (S)

    #Squeezing phase

    outputstring = np.chararray(400 * inputnum)
    gpu_table = cl.Buffer(context, cl.mem_flags.READ_ONLY | cl.mem_flags.COPY_HOST_PTR, hostbuf=S)
    gpu_string = cl.Buffer(context, cl.mem_flags.READ_WRITE, 144*8 * inputnum)
    program.convert_table_to_str(queue,global_size,local_size,gpu_table, gpu_string,np.uint64(5),np.uint64(5),np.uint64(64))
    cl.enqueue_copy(queue, outputstring, gpu_string, is_blocking=True)

    string = ''.join(outputstring)

    for x in range(inputnum):
        Z[x] = Z[x] + string[400 * x: 400 * x + r*2//8]


    for x in range(inputnum):
        Z[x] = Z[x][0:2*n//8]

    #output the pre-set number of bits
    return Z

if __name__ == '__main__':


    # List our platforms
    platforms = cl.get_platforms()

    # Create a context with all the devices
    devices = platforms[0].get_devices()
    context = cl.Context(devices[:2])
    #print ('This context is associated with ', len(context.devices), 'devices')

    # Create a queue for transferring data and launching computations.
    # Turn on profiling to allow us to check event times.
    queue = cl.CommandQueue(context, context.devices[0],
                            properties=cl.command_queue_properties.PROFILING_ENABLE)
    #print ('The queue is using the device:', queue.device.name)

    
    program = cl.Program(context, open('sha3.cl').read()).build(options='')


    #PARAMETERS for SHA 512
    r = 576
    c = 1024
    n = 512

    inputlist = []
    inputlist.append("")
    inputlist.append("abcd")
    inputlist.append("abcd")
    inputlist.append("abcd")
    inputlist.append("a" * 1000)

    start = time.time()
    result = Keccak(inputlist, n, r,c, program, context, queue)
    print  ("Hashing Result is")
    print (result)
    print ("Time taken is: " + str(time.time() - start))