Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python的指纹打印块_Python_Sha1_Chunking - Fatal编程技术网

使用python的指纹打印块

使用python的指纹打印块,python,sha1,chunking,Python,Sha1,Chunking,我正在尝试将一个文件分块,并为这些分块创建SHA1指纹。下面是代码。我的文件只有一行“测试”,当我从python命令提示符生成指纹时,我得到了不同的答案 >>> m = hashlib.sha1() >>> m.update("tests") >>> m.digest() '\x04\xd1?\xd0\xaao\x01\x97\xcf,\x99\x90\x19\xa6\x07\xc3l\x81\xeb\x9f' >>> &

我正在尝试将一个文件分块,并为这些分块创建SHA1指纹。下面是代码。我的文件只有一行“测试”,当我从python命令提示符生成指纹时,我得到了不同的答案

>>> m = hashlib.sha1()
>>> m.update("tests")
>>> m.digest()
'\x04\xd1?\xd0\xaao\x01\x97\xcf,\x99\x90\x19\xa6\x07\xc3l\x81\xeb\x9f'
>>> 
>>> m.hexdigest()
'04d13fd0aa6f0197cf2c999019a607c36c81eb9f'
-------------------------------------------------------------   

    import sys, os,hashlib
    kilobytes = 1024
    megabytes = kilobytes * 1000
    chunksize = int(1.4 * megabytes)                   # default: roughly a floppy
    hash = hashlib.sha1()
    def split(fromfile, todir, chunksize=chunksize):
        if not os.path.exists(todir):                  # caller handles errors
            os.mkdir(todir)                            # make dir, read/write parts
        else:
            print " path exists"
          ##for fname in os.listdir(todir):            # delete any existing files
          ## os.remove(os.path.join(todir, fname))
        partnum = 0
        input = open(fromfile, 'rb')                   # use binary mode on Windows
        while 1:                                       # eof=empty string from read
            chunk = input.read(chunksize)              # get next part <= chunksize
            print "chunk=",chunk,"\n"
            if not chunk: break
            partnum  = partnum+1
            filename = os.path.join(todir, ('hashpart%04d' % partnum))
            fileobj  = open(filename, 'wb')
            print "chunk before hashin is =",chunk, "of type ",type(chunk)
            hash.update(chunk)
            print "finger print is ",hash.hexdigest()
            fileobj.write(hash.digest())
            fileobj.close()
        input.close(  )
        assert partnum <= 9999                         # join sort fails if 5 digits
        return partnum

-----------------------------------------------------------

but with the code written above it gives me

chunk before hashin is = tests
of type  <type 'str'>
finger print is  64853233b4bd86fc53565e1383f2b19b6ede2995

Can someone help me?
>m=hashlib.sha1()
>>>m.更新(“测试”)
>>>m.文摘()
“\x04\xd1?\xd0\xaao\x01\x97\xcf\x99\x90\x19\xa6\x07\xc3l\x81\xeb\x9f”
>>> 
>>>m.hexdigest()
‘04D13FD0AA6F0197CF2C99901A607C36C81EB9F’
-------------------------------------------------------------   
导入sys、os、hashlib
千字节=1024
兆字节=千字节*1000
chunksize=int(1.4兆字节)#默认值:大约是一张软盘
hash=hashlib.sha1()
def拆分(fromfile,todir,chunksize=chunksize):
如果不存在os.path.exists(todir):#调用方处理错误
os.mkdir(todir)#生成dir,读/写部分
其他:
打印“路径存在”
##对于os.listdir(todir)中的fname:#删除任何现有文件
##os.remove(os.path.join(todir,fname))
partnum=0
输入=打开(来自文件“rb”)#在Windows上使用二进制模式
而1:#eof=读取的空字符串
chunk=input.read(chunksize)#获取下一部分您的文件是“tests\n”。这就是为什么你的打印声明跨越多行。添加新行,得到正确的哈希值

>>> m.update("tests\n")
>>> m.hexdigest()
'64853233b4bd86fc53565e1383f2b19b6ede2995'