hashlib

_sha import in python hashlib

六眼飞鱼酱① 提交于 2019-12-06 05:22:14
问题 Well, today I was checking the hashlib module in python, but then I found something that I still can't figure out. Inside this python module, there is an import that I can't follow. I goes like this: def __get_builtin_constructor(name): if name in ('SHA1', 'sha1'): import _sha return _sha.new I tried to import the _sha module from a python shell, but is seems that it cannot be reached that way.My first guess is that it's a C module, but I'm not sure. So tell me guys, do you know where is that

Convert string to random but deterministically repeatable uniform probability

依然范特西╮ 提交于 2019-12-06 01:16:46
问题 How do I convert a string, e.g. a user ID plus salt, to a random looking but actually a deterministically repeatable uniform probability in the semi-open range [0.0, 1.0)? This means that the output is ≥ 0.0 and < 1.0. The output distribution must be uniform irrespective of the input distribution. For example, if the input string is 'a3b2Foobar', the output probability could repeatably be 0.40341504. Cross-language and cross-platform algorithmic reproducibility is desirable. I'm inclined to

Is there a faster way (than this) to calculate the hash of a file (using hashlib) in Python?

醉酒当歌 提交于 2019-12-05 22:27:52
My current approach is this: def get_hash(path=PATH, hash_type='md5'): func = getattr(hashlib, hash_type)() with open(path, 'rb') as f: for block in iter(lambda: f.read(1024*func.block_size, b''): func.update(block) return func.hexdigest() It takes about 3.5 seconds to calculate the md5sum of a 842MB iso file on an i5 @ 1.7 GHz. I have tried different methods of reading the file, but all of them yield slower results. Is there, perhaps, a faster solution? EDIT: I replaced 2**16 (inside the f.read() ) with 1024*func.block_size , since the default block_size for most hashing functions supported

Convert integer to a random but deterministically repeatable choice

白昼怎懂夜的黑 提交于 2019-12-05 06:41:44
How do I convert an unsigned integer (representing a user ID) to a random looking but actually a deterministically repeatable choice? The choice must be selected with equal probability (irrespective of the distribution of the the input integers). For example, if I have 3 choices, i.e. [0, 1, 2] , the user ID 123 may always be randomly assigned choice 2, whereas the user ID 234 may always be assigned choice 1. Cross-language and cross-platform algorithmic reproducibility is desirable. I'm inclined to use a hash function and modulo unless there is a better way. Here is what I have: >>> num

Hashing raw bytes in Python and Java produces different results

為{幸葍}努か 提交于 2019-12-05 04:10:40
问题 I'm trying to replicate the behavior of a Python 2.7 function in Java , but I'm getting different results when running a (seemingly) identical sequence of bytes through a SHA-256 hash. The bytes are generated by manipulating a very large integer (exactly 2048 bits long) in a specific way (2nd line of my Python code example). For my examples, the original 2048-bit integer is stored as big_int and bigInt in Python and Java respectively, and both variables contain the same number. Python2 code I

_sha import in python hashlib

扶醉桌前 提交于 2019-12-04 11:35:25
Well, today I was checking the hashlib module in python, but then I found something that I still can't figure out. Inside this python module, there is an import that I can't follow. I goes like this: def __get_builtin_constructor(name): if name in ('SHA1', 'sha1'): import _sha return _sha.new I tried to import the _sha module from a python shell, but is seems that it cannot be reached that way.My first guess is that it's a C module, but I'm not sure. So tell me guys, do you know where is that module? How do they import it? Actually, the _sha module is provided by shamodule.c and _md5 is

Hashing a file in Python

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 07:31:50
问题 I want python to read to the EOF so I can get an appropriate hash, whether it is sha1 or md5. Please help. Here is what I have so far: import hashlib inputFile = raw_input("Enter the name of the file:") openedFile = open(inputFile) readFile = openedFile.read() md5Hash = hashlib.md5(readFile) md5Hashed = md5Hash.hexdigest() sha1Hash = hashlib.sha1(readFile) sha1Hashed = sha1Hash.hexdigest() print "File Name: %s" % inputFile print "MD5: %r" % md5Hashed print "SHA1: %r" % sha1Hashed 回答1: TL;DR

Convert string to random but deterministically repeatable uniform probability

泄露秘密 提交于 2019-12-04 06:00:44
How do I convert a string, e.g. a user ID plus salt, to a random looking but actually a deterministically repeatable uniform probability in the semi-open range [0.0, 1.0)? This means that the output is ≥ 0.0 and < 1.0. The output distribution must be uniform irrespective of the input distribution. For example, if the input string is 'a3b2Foobar', the output probability could repeatably be 0.40341504. Cross-language and cross-platform algorithmic reproducibility is desirable. I'm inclined to use a hash function unless there is a better way. Here is what I have: >>> in_str = 'a3b2Foobar' >>>

HMAC signing requests in Python

你说的曾经没有我的故事 提交于 2019-12-04 04:22:05
I'm trying to create an HMAC-SHA512 signed request for an API call in Python 3.4 using the requests library. I'm trying to follow docs, but am hitting this error: AttributeError: '_hashlib.HASH' object has no attribute 'new' Here's some code. It's failing with the error on the hmac constructor. It's fine if I try and pass hashlib.md5() or omit the digest parameter entirely. I'm not sure if I'm signing the request properly afterwards as I haven't got that far yet. The docs for the service I'm trying to use say to sign the URL with my secret. I need this to be a byte string for this to work.

Hashing raw bytes in Python and Java produces different results

一个人想着一个人 提交于 2019-12-03 22:17:18
I'm trying to replicate the behavior of a Python 2.7 function in Java , but I'm getting different results when running a (seemingly) identical sequence of bytes through a SHA-256 hash. The bytes are generated by manipulating a very large integer (exactly 2048 bits long) in a specific way (2nd line of my Python code example). For my examples, the original 2048-bit integer is stored as big_int and bigInt in Python and Java respectively, and both variables contain the same number. Python2 code I'm trying to replicate: raw_big_int = ("%x" % big_int).decode("hex") buff = struct.pack(">i", len(raw