Sumation hash and key value in python
Question:
I try to sum hash + Key to gain new hash value.
The key value:
a= "111631279578723877696242174"
The Hash Value by SHA-256:
b = "7de8c9d1ce09fd2554fc0468ae52d5144800d3ae3ae9b075a3ba4494e5e55f50"
My idea is to convert them to a binary value and sum the two but I got an error:
V1= list(map(bin, bytearray(a, "utf-8")))
V2=list(map(bin, bytearray(b, "utf-8")))
sum = bin(int(V1, 2) + int(V2, 2))
The error:
TypeError: int() can’t convert non-string with explicit base
How can I solve this error, or if any another way to combine the key and hash?
Answers:
Original Answer
i came up with this method to combine them:
>>> key=b'pvvFgKElhtkbDt0Lt7gOlcR4Kzwohz1UWZ8UzM2FKLg='
>>> _hash=b'gAAAAABiQX2cDIWlX4ySuBjkZsMyO09RiS0HJXEb7NvqfRbwj2W0rxko_fp1murmm6T1ZaE-dSkDOGNm1yA7bsq-rjXeiM-ckg=='
>>> hex(int.from_bytes(key,'little')+int.from_bytes(_hash,'little')).removeprefix("0x")
result:
'3d3d6734373347716c355342313549617a2d4b697357527347384b495f49723132635a6c62436a6b6768586e66486950735265487650723783a99c90a57495f0aa819ea5c96abd99d6cfcb7c78aaabb2b39c8fbec175bdb38f9eccb9d5878ca887b7b7d7'
EDIT
If you want to combine them and still get the hash back when you have the key, you can simply use xor. This is used in many symmetric encryption algorithms because you can get the original value by using xor on the encrypted value and the key.
in short the fundamental properties that we care about for the xor value are:
encrypted = original_value ^ key
original_value = encrypted ^ key
If you have the key then you can get the original value using the encrypted one.
Concrete Implementation
This is an implementation of the idea I just wrote.
import math
key=b'pvvFgKElhtkbDt0Lt7gOlcR4Kzwohz1UWZ8UzM2FKLg='
_hash=b'hx05hxc5x14xd4*x10x81xf9x84nwxe8xc7Mx8bHxf3xc32x97xc2x9fx88@xb5xffH] v'
encrypted_int = int.from_bytes(_hash,'little') ^ int.from_bytes(key,'little')
lenght_bytes_encrypted = math.ceil(math.log(encrypted_int,0xFF))
print("encrypted form: ",encrypted_int.from_bytes(lenght_bytes_encrypted,'little')
#transforming back to _hash
hash_int = encrypted_int ^ int.from_bytes(key,'little')
lenght_bytes = math.ceil(math.log(hash_int,0xFF))
normal = hash_int.to_bytes(lenght_bytes,'little')
print("converted to base form:",normal)
Output
encrypted form: b"x1777x07&nx07x059,Yx01x00=g ,x03x1ex1cx19!8_x11t:x16'Jx08x07>tx08x1d0x15w$|x02x11LfRbwj2W0rxko_fp1murmm6T1ZaE-dSkDOGNm1yA7bsq-rjXeiM-ckg=="
converted base form: b'pvvFgKElhtkbDt0Lt7gOlcR4Kzwohz1UWZ8UzM2FKLg='
I try to sum hash + Key to gain new hash value.
The key value:
a= "111631279578723877696242174"
The Hash Value by SHA-256:
b = "7de8c9d1ce09fd2554fc0468ae52d5144800d3ae3ae9b075a3ba4494e5e55f50"
My idea is to convert them to a binary value and sum the two but I got an error:
V1= list(map(bin, bytearray(a, "utf-8")))
V2=list(map(bin, bytearray(b, "utf-8")))
sum = bin(int(V1, 2) + int(V2, 2))
The error:
TypeError: int() can’t convert non-string with explicit base
How can I solve this error, or if any another way to combine the key and hash?
Original Answer
i came up with this method to combine them:
>>> key=b'pvvFgKElhtkbDt0Lt7gOlcR4Kzwohz1UWZ8UzM2FKLg='
>>> _hash=b'gAAAAABiQX2cDIWlX4ySuBjkZsMyO09RiS0HJXEb7NvqfRbwj2W0rxko_fp1murmm6T1ZaE-dSkDOGNm1yA7bsq-rjXeiM-ckg=='
>>> hex(int.from_bytes(key,'little')+int.from_bytes(_hash,'little')).removeprefix("0x")
result:
'3d3d6734373347716c355342313549617a2d4b697357527347384b495f49723132635a6c62436a6b6768586e66486950735265487650723783a99c90a57495f0aa819ea5c96abd99d6cfcb7c78aaabb2b39c8fbec175bdb38f9eccb9d5878ca887b7b7d7'
EDIT
If you want to combine them and still get the hash back when you have the key, you can simply use xor. This is used in many symmetric encryption algorithms because you can get the original value by using xor on the encrypted value and the key.
in short the fundamental properties that we care about for the xor value are:
encrypted = original_value ^ key
original_value = encrypted ^ key
If you have the key then you can get the original value using the encrypted one.
Concrete Implementation
This is an implementation of the idea I just wrote.
import math
key=b'pvvFgKElhtkbDt0Lt7gOlcR4Kzwohz1UWZ8UzM2FKLg='
_hash=b'hx05hxc5x14xd4*x10x81xf9x84nwxe8xc7Mx8bHxf3xc32x97xc2x9fx88@xb5xffH] v'
encrypted_int = int.from_bytes(_hash,'little') ^ int.from_bytes(key,'little')
lenght_bytes_encrypted = math.ceil(math.log(encrypted_int,0xFF))
print("encrypted form: ",encrypted_int.from_bytes(lenght_bytes_encrypted,'little')
#transforming back to _hash
hash_int = encrypted_int ^ int.from_bytes(key,'little')
lenght_bytes = math.ceil(math.log(hash_int,0xFF))
normal = hash_int.to_bytes(lenght_bytes,'little')
print("converted to base form:",normal)
Output
encrypted form: b"x1777x07&nx07x059,Yx01x00=g ,x03x1ex1cx19!8_x11t:x16'Jx08x07>tx08x1d0x15w$|x02x11LfRbwj2W0rxko_fp1murmm6T1ZaE-dSkDOGNm1yA7bsq-rjXeiM-ckg=="
converted base form: b'pvvFgKElhtkbDt0Lt7gOlcR4Kzwohz1UWZ8UzM2FKLg='