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
Related
For example, if I take “Hello World” and go paste it into some online hashing website, or by using this python code:
from hashlib import sha256
print(sha256('Hello World'.encode()).hexdigest())
it would return "a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e" which represent its hash value.
But what i'm looking for is some document or whatever that allows me to go manually from that text to hash
I want to add file identifiers to a script I am creating that are short and unique. The identifier needs to be related to the input arguments given to the script from the CLI, so that the same identifier gets generated from the same arguments always.
I thought of hashing them with an MD5 hash that gets as input all the arguments provided by the user in the CLI:
import sys
import hashlib
file_id = hashlib.md5(str(sys.argv).encode("utf-8"))
print(file_id)
This only gives me an object description & memory location:
<md5 HASH object # 0x0000324294...>
If I could get the actual hash I could extract the first 20 characters or so and it would be good enough for my use case.
You can use the hexdigest method like so:
import sys
import hashlib
my_hash = hashlib.md5()
my_hash.update(str(sys.argv).encode("utf-8"))
print(my_hash.hexdigest())
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 am trying to build a base64 encoded sha1 hash in go but the result i am getting is very different to the results of other programming languages
package main
import (
"crypto/sha1"
"encoding/base64"
"fmt"
)
func main() {
c := sha1.New()
input := []byte("hello")
myBytes := c.Sum(input)
fmt.Println(base64.StdEncoding.EncodeToString(base64.StdPadding))
}
This Go Code prints out aGVsbG/aOaPuXmtLDTJVv++VYBiQr9gHCQ==
My Python Code Looks like this
import hashlib
import base64
print(base64.b64encode(hashlib.sha1('hello').digest()))
And outputs qvTGHdzF6KLavt4PO0gs2a6pQ00=
My bash command for comparison looks like this
echo -n hello| openssl dgst -sha1 -binary |base64
And outputs this qvTGHdzF6KLavt4PO0gs2a6pQ00=
Which lets me assume that the python code is doing everything correct.
But why does go prints another result.
Where is my mistake?
Thnx in advance
You use the standard lib in a completely wrong way. Don't assume what a method / function does, always read the docs if it's new to you.
sha1.New() returns a hash.Hash. Its Sum() method is not to calculate the hash value, but to get the current hash result, it does not change the underlying hash state.
hash.Hash implements io.Writer, and to calculate the hash of some data, you have to write that data into it. Hash.Sum() takes an optional slice if you already have one allocated, to write the result (the hash) to it. Pass nil if you want it to allocate a new one.
Also base64.StdEncoding.EncodeToString() expects the byte data (byte slice) you want to convert to base64, so you have to pass the checksum data to it. In your code you didn't tell EncodeToString() what to encode.
Working example:
c := sha1.New()
input := []byte("hello")
c.Write(input)
sum := c.Sum(nil)
fmt.Println(base64.StdEncoding.EncodeToString(sum))
Output is as expected (try it on the Go Playground):
qvTGHdzF6KLavt4PO0gs2a6pQ00=
Note that the crypto/sha1 package also has a handy sha1.Sum() function which does this in one step:
input := []byte("hello")
sum := sha1.Sum(input)
fmt.Println(base64.StdEncoding.EncodeToString(sum[:]))
Output is the same. Try it on the Go Playground.
There is an example of how to use it properly. You should do:
c := sha1.New()
io.WriteString(c, "hello")
myBytes := c.Sum(nil)
fmt.Println(base64.StdEncoding.EncodeToString(myBytes))
https://play.golang.org/p/sELsWTcrdd
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()