当前位置: 首页 > 面试题库 >

持续使用sha256哈希对象?

湛玄裳
2023-03-14
问题内容

我需要一个Python / C / C ++ / Java实现,可以 暂停散列进步商店 ,在这样一个文件进度的进展是 可恢复
从该文件在稍后阶段。

无论上面列出的是哪种语言编写的,它都应该可以在Python中正常工作。建议您提供它以便与“
hashlib”一起良好地工作,但这不是必需的。同样,如果已经存在这样的事物,那么到此的链接就足够了。

对于一个想法 ,您的实现应该实现什么。

import hashlib
import hashpersist #THIS IS NEEDED.

sha256 = hashlib.sha256("Hello ")
hashpersist.save_state(sha256, open('test_file', 'w'))

sha256_recovered = hashpersist.load_state(open('test_file', 'r'))
sha256_recovered.update("World")
print sha256_recovered.hexdigest()

这应该提供与使用标准sha256函数对“ Hello World”进行简单哈希处理时相同的输出。

a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e

问题答案:

事实证明,比起我想重写hashlib至少要恢复SHA-256部分要容易得多。我花了一些时间玩使用OpenSSL加密库的C代码,但是后来我意识到我并不需要所有这些东西,我可以只使用ctypes。

rehash.py

#! /usr/bin/env python

''' A resumable implementation of SHA-256 using ctypes with the OpenSSL crypto library

    Written by PM 2Ring 2014.11.13
'''

from ctypes import *

SHA_LBLOCK = 16
SHA256_DIGEST_LENGTH = 32

class SHA256_CTX(Structure):
    _fields_ = [
        ("h", c_long * 8),
        ("Nl", c_long),
        ("Nh", c_long),
        ("data", c_long * SHA_LBLOCK),
        ("num", c_uint),
        ("md_len", c_uint)
    ]

HashBuffType = c_ubyte * SHA256_DIGEST_LENGTH

#crypto = cdll.LoadLibrary("libcrypto.so")
crypto = cdll.LoadLibrary("libeay32.dll" if os.name == "nt" else "libssl.so")

class sha256(object):
    digest_size = SHA256_DIGEST_LENGTH

    def __init__(self, datastr=None):
        self.ctx = SHA256_CTX()
        crypto.SHA256_Init(byref(self.ctx))
        if datastr:
            self.update(datastr)

    def update(self, datastr):
        crypto.SHA256_Update(byref(self.ctx), datastr, c_int(len(datastr)))

    #Clone the current context
    def _copy_ctx(self):
        ctx = SHA256_CTX()
        pointer(ctx)[0] = self.ctx
        return ctx

    def copy(self):
        other = sha256()
        other.ctx = self._copy_ctx()
        return other

    def digest(self):
        #Preserve context in case we get called before hashing is
        # really finished, since SHA256_Final() clears the SHA256_CTX
        ctx = self._copy_ctx()
        hashbuff = HashBuffType()
        crypto.SHA256_Final(hashbuff, byref(self.ctx))
        self.ctx = ctx
        return str(bytearray(hashbuff))

    def hexdigest(self):
        return self.digest().encode('hex')

#Tests
def main():
    import cPickle
    import hashlib

    data = ("Nobody expects ", "the spammish ", "imposition!")

    print "rehash\n"

    shaA = sha256(''.join(data))
    print shaA.hexdigest()
    print repr(shaA.digest())
    print "digest size =", shaA.digest_size
    print

    shaB = sha256()
    shaB.update(data[0])
    print shaB.hexdigest()

    #Test pickling
    sha_pickle = cPickle.dumps(shaB, -1)
    print "Pickle length:", len(sha_pickle)
    shaC = cPickle.loads(sha_pickle)

    shaC.update(data[1])
    print shaC.hexdigest()

    #Test copying. Note that copy can be pickled
    shaD = shaC.copy()

    shaC.update(data[2])
    print shaC.hexdigest()


    #Verify against hashlib.sha256()
    print "\nhashlib\n"

    shaD = hashlib.sha256(''.join(data))
    print shaD.hexdigest()
    print repr(shaD.digest())
    print "digest size =", shaD.digest_size
    print

    shaE = hashlib.sha256(data[0])
    print shaE.hexdigest()

    shaE.update(data[1])
    print shaE.hexdigest()

    #Test copying. Note that hashlib copy can NOT be pickled
    shaF = shaE.copy()
    shaF.update(data[2])
    print shaF.hexdigest()


if __name__ == '__main__':
    main()

resumable_SHA-256.py

#! /usr/bin/env python

''' Resumable SHA-256 hash for large files using the OpenSSL crypto library

    The hashing process may be interrupted by Control-C (SIGINT) or SIGTERM.
    When a signal is received, hashing continues until the end of the
    current chunk, then the current file position, total file size, and
    the sha object is saved to a file. The name of this file is formed by
    appending '.hash' to the name of the file being hashed.

    Just re-run the program to resume hashing. The '.hash' file will be deleted
    once hashing is completed.

    Written by PM 2Ring 2014.11.14
'''

import cPickle as pickle
import os
import signal
import sys

import rehash

quit = False

blocksize = 1<<16   # 64kB
blocksperchunk = 1<<8

chunksize = blocksize * blocksperchunk

def handler(signum, frame):
    global quit
    print "\nGot signal %d, cleaning up." % signum
    quit = True


def do_hash(fname, filesize):
    hashname = fname + '.hash'
    if os.path.exists(hashname):
        with open(hashname, 'rb') as f:
            pos, fsize, sha = pickle.load(f)
        if fsize != filesize:
            print "Error: file size of '%s' doesn't match size recorded in '%s'" % (fname, hashname)
            print "%d != %d. Aborting" % (fsize, filesize)
            exit(1)
    else:
        pos, fsize, sha = 0, filesize, rehash.sha256()

    finished = False
    with open(fname, 'rb') as f:
        f.seek(pos)
        while not (quit or finished):
            for _ in xrange(blocksperchunk):
                block = f.read(blocksize)
                if block == '':
                    finished = True
                    break
                sha.update(block)

            pos += chunksize
            sys.stderr.write(" %6.2f%% of %d\r" % (100.0 * pos / fsize, fsize))
            if finished or quit:
                break

    if quit:
        with open(hashname, 'wb') as f:
            pickle.dump((pos, fsize, sha), f, -1)
    elif os.path.exists(hashname):
        os.remove(hashname)

    return (not quit), pos, sha.hexdigest()


def main():
    if len(sys.argv) != 2:
        print "Resumable SHA-256 hash of a file."
        print "Usage:\npython %s filename\n" % sys.argv[0]
        exit(1)

    fname = sys.argv[1]
    filesize = os.path.getsize(fname)

    signal.signal(signal.SIGINT, handler)
    signal.signal(signal.SIGTERM, handler)

    finished, pos, hexdigest = do_hash(fname, filesize)
    if finished:
        print "%s  %s" % (hexdigest, fname)
    else:
        print "sha-256 hash of '%s' incomplete" % fname
        print "%s" % hexdigest
        print "%d / %d bytes processed." % (pos, filesize)


if __name__ == '__main__':
    main()

演示

import rehash
import pickle
sha=rehash.sha256("Hello ")
s=pickle.dumps(sha.ctx)
sha=rehash.sha256()
sha.ctx=pickle.loads(s)
sha.update("World")
print sha.hexdigest()

输出

a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e

编辑

rehash尽管我只在WinXP上进行了测试,但我也做了一些小的编辑以允许在Windows上工作。本libeay32.dll可以在当前目录下,或者在系统库搜索路径,如某处WINDOWS\system32。即使OpenOffice和Avira使用了.dll,我的安装也相当古老(并且几乎未使用),但找不到.dll。因此,我只是将其从Avira文件夹复制到了system32。现在,它可以完美运行。:)



 类似资料:
  • 我想向用户展示他们的客户端工具也可能生成的散列,因此我一直在比较在线散列工具。我的问题是关于它们的散列形式,因为奇怪的是,它们是不同的。 在快速搜索之后,我用5进行了测试: http://www.convertstring.com/hash/sha256 http://www.freeformatter.com/sha256-generator.html#ad-output http://onli

  • 问题内容: 我将使用密码+ salt 来运行,但是我不知道在设置MySQL数据库时需要花费多长时间。好的长度是多少? 问题答案: sha256长256位-顾名思义。 由于sha256返回一个十六进制表示,所以4个位足以编码每个字符(而不是8个,如ASCII),因此256个位将表示64个十六进制字符,因此您需要a 或什至a ,因为长度始终相同,完全没有变化。 和演示: 会给你 : 即一个包含64个字

  • 你好,所以社区我需要一些哈希专业知识: 我面临着比较两个对象列表的问题。列表的一个实例在.Net Core 2.0应用程序中,另一个在.Net 4.5.2应用程序中。 为了比较两个列表(确保它们在两个应用程序中是相同的),我想计算列表的哈希值,并在应用程序之间交换。为了计算列表的哈希值,我使用了这个答案中描述的方法。 为此,您需要列表中对象的哈希码,这就是我的问题所在:似乎众所周知的事实(1).N

  • 求求你帮帮我!我正在尝试使用pbkdf2-sha256算法哈希密码。Password=“user1”,salt=“ifo7kxyswe7fiu3bovnowg=”,hashIterations=“27500”。我知道结果。必须类似于“ZnxO94AYITK7T+OJ1PXPZTVEQ+G82LFWT6VNSTBHZPEUWZGMPRJJVKAUEXGH1IQPZWMX1SRVTUMLN/JCM8G

  • 我想获取一个SHA256散列,并使用Bouncy Castle(实际上是海绵城堡)生成一个密钥。在下面的代码摘录中,retval是SHA256Digest(字节数组)返回的值,finalKey应该是从SHA256哈希生成的键。 当我尝试运行代码时,我得到的消息是“Encoded key spec not recognized”,因此我认为retval应该采用某种特定的格式,并且我不能像现在这样直接

  • 我正在为Web应用程序制作登录系统。要将密码存储在数据库中,我正在使用sha256加密密码,如下所示: 在数据库中,我存储了用户、用户密码和用于散列和验证用户登录的salt。现在,我正在向用户发送包含您的密码的电子邮件,但是当用户收到电子邮件时,由于存储在sha256加密密码中,用户收到的是一个长字符串,而不是用户应该知道的密码。 我的问题是,我可以通过任何方法向您发送实际的用户密码和非密码加密,