fork download
  1. # -*- coding: utf-8 -*-
  2. import hashlib
  3. import struct
  4.  
  5. class DataStream(bytearray):
  6. def append(self, v, fmt='>B'):
  7. self.extend(struct.pack(fmt, v))
  8.  
  9. class HashCalculator:
  10. def calculate(self, bytearr):
  11. md = hashlib.sha256()
  12. md.update(bytearr)
  13. return md.hexdigest()[:32]
  14.  
  15. _nanoenc = "?!\"#$%&'()*+,-./0123456789:;<=> @ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ЎўЄєІіЇїАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюяčšžćśźńŭłČŠŽĆŚŽŽŬŁ№©«»±®Ґґ°™—“”’‘…–\n\r\t"
  16. class NanoEncoding:
  17. def toBytes(self, st):
  18. ds = DataStream()
  19. for c in st:
  20. if c in _nanoenc:
  21. ds.append(_nanoenc.index(c))
  22. else:
  23. ds.append(_nanoenc.index('?'))
  24. return ds
  25.  
  26. def toString(self, dataSt):
  27. arr = []
  28. for b in dataSt:
  29. if b < len(_nanoenc):
  30. arr.append(_nanoenc[b])
  31. else:
  32. arr.append('?')
  33. return ''.join(arr)
  34.  
  35. nc = NanoEncoding()
  36. hs = HashCalculator()
  37. print(hs.calculate(nc.toBytes("test")))
Success #stdin #stdout 0.02s 11472KB
stdin
Standard input is empty
stdout
94ee059335e587e501cc4bf90613e081