To better understand how aerospike works I want to write a python script that generates the the key from namespace, set, and userkey.
I know that aerospike is using RIPEMD-160 hash and it looks like that for key generation it does not use namspace, but i wasn’t able to write python script that creates the same key as aerospike creates.
Here is the code example that is generating key with aerospike:
<code>#!/usr/local/bin/python
'hosts': [ ('aerospike', 3000) ],
'policies': {"write": {"key": aerospike.POLICY_KEY_SEND}}
client = aerospike.client(config).connect()
print("Failed to connect to the cluster with", config['hosts'])
key = ('test', 'demo', 'one')
client.put(key, {'bin1': 'value1'})
print("error: {0}".format(e), file=sys.stderr)
(key, metadata, record) = client.get(key)
print("key from aerospike:", key)
<code>#!/usr/local/bin/python
import sys
import aerospike
config = {
'hosts': [ ('aerospike', 3000) ],
'policies': {"write": {"key": aerospike.POLICY_KEY_SEND}}
}
try:
client = aerospike.client(config).connect()
except:
print("Failed to connect to the cluster with", config['hosts'])
sys.exit(1)
key = ('test', 'demo', 'one')
print('key:', key)
try:
client.put(key, {'bin1': 'value1'})
except Exception as e:
print("error: {0}".format(e), file=sys.stderr)
sys.exit(1)
(key, metadata, record) = client.get(key)
print("key from aerospike:", key)
</code>
#!/usr/local/bin/python
import sys
import aerospike
config = {
'hosts': [ ('aerospike', 3000) ],
'policies': {"write": {"key": aerospike.POLICY_KEY_SEND}}
}
try:
client = aerospike.client(config).connect()
except:
print("Failed to connect to the cluster with", config['hosts'])
sys.exit(1)
key = ('test', 'demo', 'one')
print('key:', key)
try:
client.put(key, {'bin1': 'value1'})
except Exception as e:
print("error: {0}".format(e), file=sys.stderr)
sys.exit(1)
(key, metadata, record) = client.get(key)
print("key from aerospike:", key)
This is the output of this script:
<code>root@d1716cafa3fb:/app# ./a.py
key: ('test', 'demo', 'one')
key from aerospike: ('test', 'demo', None, bytearray(b'xe4Ix9bJxa5r1fLxe0x19xe5xc2x9d"Xrxdbxbfx03'))
<code>root@d1716cafa3fb:/app# ./a.py
key: ('test', 'demo', 'one')
key from aerospike: ('test', 'demo', None, bytearray(b'xe4Ix9bJxa5r1fLxe0x19xe5xc2x9d"Xrxdbxbfx03'))
root@d1716cafa3fb:/app#
</code>
root@d1716cafa3fb:/app# ./a.py
key: ('test', 'demo', 'one')
key from aerospike: ('test', 'demo', None, bytearray(b'xe4Ix9bJxa5r1fLxe0x19xe5xc2x9d"Xrxdbxbfx03'))
root@d1716cafa3fb:/app#
And here is what I see in aerospike client after I execute this script:
<code>aql> set record_print_metadata true
RECORD_PRINT_METADATA = true
+-------+----------+--------------------------------+--------+---------+-------+
| PK | bin1 | {edigest} | {set} | {ttl} | {gen} |
+-------+----------+--------------------------------+--------+---------+-------+
| "one" | "value1" | "5EmbSqVyMWZM4Bnlwp0iWHLbvwM=" | "demo" | 2591964 | 1 |
+-------+----------+--------------------------------+--------+---------+-------+
1 row in set (0.024 secs)
<code>aql> set record_print_metadata true
RECORD_PRINT_METADATA = true
aql> select * from test
+-------+----------+--------------------------------+--------+---------+-------+
| PK | bin1 | {edigest} | {set} | {ttl} | {gen} |
+-------+----------+--------------------------------+--------+---------+-------+
| "one" | "value1" | "5EmbSqVyMWZM4Bnlwp0iWHLbvwM=" | "demo" | 2591964 | 1 |
+-------+----------+--------------------------------+--------+---------+-------+
1 row in set (0.024 secs)
</code>
aql> set record_print_metadata true
RECORD_PRINT_METADATA = true
aql> select * from test
+-------+----------+--------------------------------+--------+---------+-------+
| PK | bin1 | {edigest} | {set} | {ttl} | {gen} |
+-------+----------+--------------------------------+--------+---------+-------+
| "one" | "value1" | "5EmbSqVyMWZM4Bnlwp0iWHLbvwM=" | "demo" | 2591964 | 1 |
+-------+----------+--------------------------------+--------+---------+-------+
1 row in set (0.024 secs)
So for the namespace “test”, set “demo” and userkey “one” the base64 of key is “5EmbSqVyMWZM4Bnlwp0iWHLbvwM=”.
And here is my attempt to generate the same base64:
<code>#!/usr/local/bin/python
from Crypto.Hash import RIPEMD
hash_obj = RIPEMD.new(data=message.encode('utf-8'))
hash_bytes = hash_obj.digest()
hash_base64 = base64.b64encode(hash_bytes).decode('utf-8')
print(f"RIPEMD-160 hash of '{message}' in base64 is: {hash_base64}")
<code>#!/usr/local/bin/python
from Crypto.Hash import RIPEMD
import base64
message = "demo one"
hash_obj = RIPEMD.new(data=message.encode('utf-8'))
hash_bytes = hash_obj.digest()
hash_base64 = base64.b64encode(hash_bytes).decode('utf-8')
print(f"RIPEMD-160 hash of '{message}' in base64 is: {hash_base64}")
</code>
#!/usr/local/bin/python
from Crypto.Hash import RIPEMD
import base64
message = "demo one"
hash_obj = RIPEMD.new(data=message.encode('utf-8'))
hash_bytes = hash_obj.digest()
hash_base64 = base64.b64encode(hash_bytes).decode('utf-8')
print(f"RIPEMD-160 hash of '{message}' in base64 is: {hash_base64}")
This does not work. This output
<code>root@d1716cafa3fb:/app# ./b.py
RIPEMD-160 hash of 'demo one' in base64 is: Lz0mUZgml2l+NLfnhwc+5RwlmEY=
<code>root@d1716cafa3fb:/app# ./b.py
RIPEMD-160 hash of 'demo one' in base64 is: Lz0mUZgml2l+NLfnhwc+5RwlmEY=
</code>
root@d1716cafa3fb:/app# ./b.py
RIPEMD-160 hash of 'demo one' in base64 is: Lz0mUZgml2l+NLfnhwc+5RwlmEY=
How can I modify my scrip so I get “5EmbSqVyMWZM4Bnlwp0iWHLbvwM=” ?