# -*- coding: utf-8 -*-
import hashlib
import struct
class DataStream(bytearray):
def append(self, v, fmt='>B'):
self.extend(struct.pack(fmt, v))
class HashCalculator:
def calculate(self, bytearr):
md = hashlib.sha256()
md.update(bytearr)
return md.hexdigest()[:32]
_nanoenc = "?!\"#$%&'()*+,-./0123456789:;<=> @ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ЎўЄєІіЇїАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюяčšžćśźńŭłČŠŽĆŚŽŽŬŁ№©«»±®Ґґ°™—“”’‘…–\n\r\t"
class NanoEncoding:
def toBytes(self, st):
ds = DataStream()
for c in st:
if c in _nanoenc:
ds.append(_nanoenc.index(c))
else:
ds.append(_nanoenc.index('?'))
return ds
def toString(self, dataSt):
arr = []
for b in dataSt:
if b < len(_nanoenc):
arr.append(_nanoenc[b])
else:
arr.append('?')
return ''.join(arr)
nc = NanoEncoding()
hs = HashCalculator()
print(hs.calculate(nc.toBytes("test")))
IyAtKi0gY29kaW5nOiB1dGYtOCAtKi0KaW1wb3J0IGhhc2hsaWIKaW1wb3J0IHN0cnVjdAoKY2xhc3MgRGF0YVN0cmVhbShieXRlYXJyYXkpOgoJZGVmIGFwcGVuZChzZWxmLCB2LCBmbXQ9Jz5CJyk6CgkJc2VsZi5leHRlbmQoc3RydWN0LnBhY2soZm10LCB2KSkKCmNsYXNzIEhhc2hDYWxjdWxhdG9yOgoJZGVmIGNhbGN1bGF0ZShzZWxmLCBieXRlYXJyKToKCQltZCA9IGhhc2hsaWIuc2hhMjU2KCkKCQltZC51cGRhdGUoYnl0ZWFycikKCQlyZXR1cm4gbWQuaGV4ZGlnZXN0KClbOjMyXQoKX25hbm9lbmMgPSAiPyFcIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5Ojs8PT4gQEFCQ0RFRkdISUpLTE1OT1BRUlNUVVZ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