Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.
SOLVED

Using Python to do base64 encode SHA 256 in Alteryx

brianvtran
7 - Meteor

Hello,

 

I received a request from a client who wants emails to be hashed base64 encoded SHA 256. I've tried all kinds of conversions within Alteryx with base64 encoder tool and blob converts, even trying to use formulas to convert my sha 256 into binary to be encoded base64 but it's not panning out. The client provided an example for janedoe@gmail.com but I can't match the same hash that was provided. 

 

I've seen some promise with Python but I'm completely new to the language. I'm trying to hash my emails using this code but it's not panning out. If I attempt to do the whole list, it will give me the attribute error where the Series object has no attribute encode or if I attempt to loop it in some way, it will say hashlib.hash is not iterable.

 

 

series object has no attribute encode.PNGhash.lib not iterable.PNG

The most I can do is reference a single element from the series, and do the hash, only one at a time but is there any way for me to be able to hash all the emails in the column? 

 

Any help would be greatly appreciated. 

6 REPLIES 6
NeilR
Alteryx Alumni (Retired)
brianvtran
7 - Meteor

Hey @NeilR 

 

I don't have an issue with the SHA256 part, I have some code in python and an R tool that can do the hash. My main problem is that I haven't figured out yet how to encode in base64 using python or any other means. I have tried using the base64 tools in Alteryx (base64 encoder and blob converts) but they're not returning the same results as the example given to me: 

 

janedoegmail.com --> 1hFzBkhe0OUK+rOshx6Y+BaZFR8wKBUn1j/18jNlbGk=

 

I get instead: ZDYxMTczMDY0ODVlZDBlNTBhZmFiM2FjODcxZTk4ZjgxNjk5MTUxZjMwMjgxNTI3ZDYzZmY1ZjIzMzY1NmM2OQ==

 

I have dl'ed the CreW macro but it's erroring when I run it, says I'm missing two CReW_ExpectZeroRecords.yxmc? Do I have to dl these other macros separately? They don't come together in one package? 

brianvtran
7 - Meteor

Nevermind, I was able to figure out the solution eventually.

 

Thanks, anyway. 

fpinchon
8 - Asteroid

@brianvtran 

Can you share the working code? Could be useful for anyone else attempting their own encode in Python?

 

Cheers,

Frederic

brianvtran
7 - Meteor

Sure. I pretty much used lambda expressions to apply the hash and the encoding to the entire column of Emails. I was having trouble looping the functions over the entire field because of attribute errors and hash.lib was not iterable; the best I could do was operate on one record at a time by indexing which record I wanted to look at. The next issue I faced was base64 encoding the hash correctly. Checking with online hash generators, I realized that the base64 encoding tools on Alteryx kept interpreting it as UTF-8 to encode to base64, which produced a hash different from what I expect. What I really needed was for the encoder to read it as Hex to convert to base64. I couldn't figure out a way to make that work in Alteryx so I had to outsource the labor to Python. The following codes resolved it for me:

 

This first code hashes the entire Email column for me: 

 

import hashlib

 

df['Email'] = df['Email'].astype(str)
# Apply hashing function to the column
df['Email_HASH'] = df['Email'].apply(
lambda x:
hashlib.sha256(x.encode()).hexdigest()
)

 

The 2nd code converts the hash from hex to base64:

 

from base64 import b64encode, b64decode

 

# hex -> base64
# Apply encoding function to the column
df['Email_Encode'] = df["Email_HASH"].apply(
lambda x:
b64encode(bytes.fromhex(x)).decode()
)

 

That gives me the base64 encoded SHA256 I needed. 

fpinchon
8 - Asteroid

Great, thanks for sharing. This can get helpful to get granular and try different encoding combinations.

As I am working on generating signatures for JWTs, anything helps!

Labels