I am trying to understand how the crypto algorithms RIPEMD and SHA256 work. The bitcoin method for computing PKHash is RIPEMD160(SHA256(PublicKey)).
I am trying to first implement the RIPEMD of SHA256(PublicKey).
pkHashStep1=hashlib.sha256(public_key.decode('hex')).digest()
print 'MyTransaction pkHashStep1 {}'.format(pkHashStep1)
MyTransaction pkHashStep1 ▒▒▒so▒/▒▒e▒▒▒¡▒7▒?9▒▒.▒ӟ!n▒h
This outputs a string that I cannot use directly, but the hashlib library can use this. Trying pkHashStep1.decode('hex') and bin(pkHashStep1) throws an error. How does it convert the hash to a usable hexstring/bin??
Currently, I have publicKey as input to my RipeMD method, instead of the pkHashStep1, and have to separately do
input=hashlib.sha256(publicKey.decode('hex')).hexdigest()
FYI: I know there is a ripemd method in hashlib. Suggesting I use it is not an answer
https://stackoverflow.com/a/2124289/4219479
glibdud's comment got me to the answer.
hashlib.sha256(public_key.decode('hex')).digest().encode('hex')=
hashlib.sha256(public_key.decode('hex')).hexdigest()
Related
Seems simple enough, and there are plenty of examples but I just can't seem to get hashes that verify with werkzeug.security's check_password_hash in python.
private string Generate_Passwd_Hash()
{
string _password = "Password";
string _salt = "cXoZSGKkuGWIbVdr";
SHA256 MyHash = SHA256.Create();
byte[] hashable = System.Text.Encoding.UTF8.GetBytes(_salt + _password);
byte[] resulthash = MyHash.ComputeHash(hashable);
return "sha256$" + _salt + "$" + BitConverter.ToString(resulthash).Replace("-", "").ToLower();
}
this should generate;
sha256$cXoZSGKkuGWIbVdr$7f5d63e849f0a2c0c5c2bd6ae4e45ead2ac730c853a1ed3460e227c06c567f49
but doesn't.
EDIT
Reading through the python code for generate_password_hash and it has a default number of iterations of 260000. Which is probably what I'm missing.
I never used werkzeug but I tried to reproduce your probelem.
I had read the docs of werkzeug.security.generate_password_hash and realized it is used in password validation only, and not meant to be a universal hashing algorithm.
The document clearly says
Hash a password with the given method and salt with a string of the
given length. The format of the string returned includes the method
that was used so that check_password_hash() can check the hash.
hashlib.pbkdf2_hmac is the hashing algorithm werkzeug uses internally(from now on we call it underlying algorithm). and you don't need install it because it is in standard library.
The source code of check_password_hash shows it generates a random salt before calling underlying algorithm. The salt is to protect from attacks. And it is remembered by the werkzeug framework so that check_password_hash can use to validate later.
So to summarize:
werkzeug.security.generate_password_hash only guarantee that generated hash can be validated by check_password_hash, and no more. You simply cannot(or not supposed to) try to generate same hash by other libraries or languages.
If you really want to compare the hashing algorithm in python and C#, please post another question(or update this question) that compares underlying algorithm(hashlib.pbkdf2_hmac which allow specifying salt as parameter) with C# version. Note seems in C# there's no built in algorithm for pbkdf2, see Hash Password in C#? Bcrypt/PBKDF2.
Currently I'm converting a small project from node.js/express to python/falcon.
In node to encode a contect-disposition header I'm using this library: https://github.com/jshttp/content-disposition
import contentDisposition from 'content-disposition';
resp.setHeader('Content-Disposition', contentDisposition(filename));
Looking for a way to do this in Python.
The equivalent library is called "rfc6266" (it seems it was always on top of google search results, but due to it's name I never actually looked at the page thinking it was a standard description).
import rfc6266
resp.set_header('Content-Disposition', rfc6266.build_header(filename).decode('iso-8859-1')) # the function returns bytes
So, i'm writing a python program, actually my first "big" one, that, at a certain point, needs to generate an hmac with sha-512, from a message and a key.
Let's say i have:
key = 'ff'
seed = '238973:a665f3e641d9a402df3f9d5d9a236cb8061a2437ee47de8b3c8091774484b8b3:238973'
bkey = key.encode()
bseed = seed.encode()
string = hmac.new(bKey, bSeed, 'sha3_512')
digest = string.hexdigest()
print(str(digest))
>>>92b6c5ccc654c21838cb72e932de0feaf898398300e9755b2c91553c9db0bea99a5139055b5471142b299361c6ece4b51f2f26777a6f18f0db27775625e6948f
However when i go to https://www.freeformatter.com/hmac-generator.html#ad-output it gives me another result:
249667dabafbd3fa123b9749c6b23bc90c935e30e1372f26bbab2defa791fc915d60d406efe1b165befd4a1b6dc34fcf2f0975db08a7a2938964e99ea0419dcf
which i know for a fact to be the correct one, can somebody explain me why that is? I've been looking online for hours but it seems like i'm running in circles or i don't fully grasp how the hmac works.
I would greatly appreciate your help, it's the only bit i'm missing to finish this program.
sha3_512 is not the same implementation as sha512 - the first is based on SHA-3, while the second is based on the older SHA-2 standard. Use sha512 instead when creating your hmac to use the SHA-2 version:
>>> hmac.new(b'ff', b'238973:a665f3e641d9a402df3f9d5d9a236cb8061a2437ee47de8b3c8091774484b8b3:238973', hashlib.sha512).hexdigest()
'249667dabafbd3fa123b9749c6b23bc90c935e30e1372f26bbab2defa791fc915d60d406efe1b165befd4a1b6dc34fcf2f0975db08a7a2938964e99ea0419dcf'
I'm writing a short script where I plan on getting some information about the host PC and write it to an excel workbook. I'm still learning stuff so it's nothing fancy.
I get all the data I need and can write in most of the stuff.
I can't seem to get one thing work though.
When trying the below code:
hardwareSheet.write("B7", usage + "%")
I can print out the "usage" variable only but when I add +"%" I keep getting the following error:
TypeError: unsupported operand type(s) for +: 'float' and 'str'
I'm using xlsxwriter library to crate and write excel.
hardwareSheet.write is a command allowing me to write data into sheet named hardware.
Here's how I got "usage" variable:
cpuInfo = wmi.Win32_Processor()[0]
usage = float(cpuInfo.LoadPercentage)
If I didn't parse cpuInfo.LoadPercentage to a float it would be a string.
I googled this and read that I need to parse the str into a float so I did so.Any ideas what could be wrong?
you can't add floats and strings in python. usage is a float. "%" is a string.
you should do something like:
str(usage) + '%'
This is one more option to get the same output.
'{0}%'.format(usage)
A cutting-edge solution using Python 3.6+ f-strings:
hardwareSheet.write("B7", f'{usage}%')
I used SQL to convert a social security number to MD5 hash. I am wondering if there is a module or function in python/pandas that can do the same thing.
My sql script is:
CREATE OR REPLACE FUNCTION MD5HASH(STR IN VARCHAR2) RETURN VARCHAR2 IS
V_CHECKSUM VARCHAR2(32);
BEGIN
V_CHECKSUM := LOWER(RAWTOHEX(UTL_RAW.CAST_TO_RAW(SYS.DBMS_OBFUSCATION_TOOLKIT.MD5(INPUT_ST RING => STR))));
RETURN V_CHECKSUM;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
WHEN OTHERS THEN
RAISE;
END MD5HASH;
SELECT HRPRO.MD5HASH('555555555') FROM DUAL
thanks.
I apologize, now that I read back over my initial question it is quite confusing.
I have a data frame that contains the following headings:
df[['ssno','regions','occ_ser','ethnicity','veteran','age','age_category']][:10]
Where ssno is personal information that I would like to convert to an md5 hash number and then create a new column into the dataframe.
thanks... sorry for the confusion.
Right now I have to send my file to Oracle and then convert the ssn to hash and then export back out so that I can continue working with it in Pandas. I want to eliminate this step.
Using the standard hashlib module:
import hashlib
hash = hashlib.md5()
hash.update('555555555')
print hash.hexdigest()
output
3665a76e271ada5a75368b99f774e404
As mentioned in timkofu's comment, you can also do this more simply, using
print hashlib.md5('555555555').hexdigest()
The .update() method is useful when you want to generate a checksum in stages. Please see the hashlib documentation (or the Python 3 version) for further details.
hashlib with md5 might be of your interest.
import hashlib
hashlib.md5("Nobody inspects the spammish repetition").hexdigest()
output:
bb649c83dd1ea5c9d9dec9a18df0ffe9
Constructors for hash algorithms that are always present in this module are md5(), sha1(), sha224(), sha256(), sha384(), and sha512().
If you want more condensed result, then you may try sha series
output for sha224:
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
For more details : hashlib