The little things give you away... A collection of various small helper stuff
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

46 satır
1.9 KiB

  1. #!/usr/bin/env python3
  2. # The SHA1 UUID stuff in Ruby is actually more complicated. Everything's right until the `head -c32`, but then Ruby transforms it into an integer in a quite peculiar way: https://github.com/sporkmonger/uuidtools/blob/a10724236cefd922ee5cd3de7695fb6e5fd703f5/lib/uuidtools.rb#L480-L494
  3. # Ruby code: ArchiveBot lib/job.rb + https://github.com/sporkmonger/uuidtools/blob/a10724236cefd922ee5cd3de7695fb6e5fd703f5/lib/uuidtools.rb#L688-L691
  4. # Takes the SHA-1 hash of the namespace (as raw bytes) and the name, truncates it to 32 hex chars, creates a new UUID from it, transforms two fields, converts it to a bigint, and formats it in base-36
  5. # sed/sha1sum/head/bash-based version missing the time_hi_and_version and clock_seq_hi_and_reserved modification
  6. #{ echo -n '82244de1-c354-4c89-bf2b-f153ce23af43' | sed 's,-,,g' | xxd -r -p; echo -n 'https://transfer.notkiska.pw/sDu6C/marwilliamson-twitter.txt'; } | sha1sum | head -c32 | { read -r hash; BASE36=($(echo {0..9} {a..z})); for i in $(bc <<< "obase=32; ibase=16; ${hash^^}" | tr -d '\\\n'); do echo -n ${BASE36[$((10#$i))]}; done; }; echo
  7. import hashlib
  8. import sys
  9. import uuid
  10. url = sys.argv[1] # Assume that it's normalised already
  11. # Calculate hash
  12. h = hashlib.sha1()
  13. h.update(bytes.fromhex('82244de1-c354-4c89-bf2b-f153ce23af43'.replace('-', '')))
  14. h.update(url.encode('ascii'))
  15. h = h.hexdigest()
  16. # Create and transform UUID object
  17. u = uuid.UUID(h[:32])
  18. f = list(u.fields)
  19. f[2] &= 0x0FFF
  20. f[2] |= (5 << 12)
  21. f[3] &= 0x3F;
  22. f[3] |= 0x80;
  23. # Turn it into an int
  24. #i = (f[0] << 96) + (f[1] << 80) + (f[2] << 64) + (f[3] << 56) + (f[4] << 48) + f[5]
  25. i = uuid.UUID(fields = f).int
  26. # Convert to base-36
  27. def int_to_base36(num):
  28. # https://stackoverflow.com/a/31746873
  29. assert num >= 0
  30. digits = '0123456789abcdefghijklmnopqrstuvwxyz'
  31. res = ''
  32. while not res or num > 0:
  33. num, i = divmod(num, 36)
  34. res = digits[i] + res
  35. return res
  36. print(int_to_base36(i))