Created
February 25, 2017 11:34
-
-
Save utgwkk/7d6be80c34b34f7cdf378487f28a94c3 to your computer and use it in GitHub Desktop.
Generate tripcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: shift-jis | |
import re | |
import string | |
from crypt import crypt | |
from base64 import b64encode | |
from hashlib import sha1 | |
def generate_trip(tripstr): | |
''' | |
>>> generate_trip('#istrip') | |
u'\u25c6/WG5qp963c' | |
>>> generate_trip('#ニコニコ') | |
u'\u25c6pA8Bpf.Qvk' | |
''' | |
if len(tripstr[1:]) >= 12: | |
mark = tripstr[0] | |
if mark == '#' or mark == '$': | |
m = re.match(r'^#([0-9a-fA-F]{16})([\./0-9A-Za-z]{0,2})$', tripstr) | |
if m: | |
trip = crypt(str(int(m.group(1))), m.group(2) + '..')[-10:] | |
else: | |
trip = '???' | |
else: | |
m = sha1(tripstr[1:]) | |
trip = b64encode(m.digest())[:12] | |
trip = trip.replace('+', '.') | |
else: | |
tripkey = tripstr[1:] | |
salt = (tripkey + 'H.')[1:3] | |
salt = re.sub(r'[^\.-z]', '.', salt) | |
salt = salt.translate(string.maketrans(':;<=>?@[\\]^_`', 'ABCDEFGabcdef')) | |
trip = crypt(tripkey, salt) | |
trip = trip[-10:] | |
trip = '◆' + trip | |
retval = unicode(trip, encoding='shift-jis') | |
return retval | |
if __name__ == '__main__': | |
import doctest | |
doctest.testmod() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment