Hexadecimal handling in Python 3

When using hashes etc. for security, even if it is displayed as it is with print, it will be garbled and can not be read, so a memo when I was playing around with it.

str type and bytes type

'test' == b'test'

Since it is an srt type and a bytes type, of course it is different, so False is returned.

Conversion to str-> bytes

'test'.encode('utf-8') == b'test'

True is returned because str is converted to bytes type.

Returns a hexadecimal representation of binary data

from binascii import hexlify
hexlify(b'A')

"B'41'" is returned.

Returns the binary data represented by the hexadecimal string hexstr

from binascii import unhexlify
unhexlify(b'41')

Since it is the opposite of hexlify, "b'A'" is returned.

I want to remove b''

b'A'.decode('utf-8')

Try using hash based on the above

import hashlib

from binascii import hexlify

origin = 'Ah'
encoded_origin = origin.encode('utf-8')
hash_obj = hashlib.sha256()
hash_obj.update(encoded_origin)
digest = hash_obj.digest()
print(digest)                 #Humans can't read
print(hexlify(digest))        #Human readable

Recommended Posts

Hexadecimal handling in Python 3
Handling json in python
Relative url handling in python
Handling of JSON files in Python
Handling timezones in Python (datetime, pytz)
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
Python Error Handling
Python exception handling
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
Python timezone handling
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Python exception handling
Flatten in python
flatten in python
Sample for handling eml files in Python
Module import and exception handling in python
Sorted list in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Edit fonts in Python
Singleton pattern in Python
File operations in Python