def test_alphabet_without_standard_separators(self): h = Hashids( alphabet='abdegjklmnopqrvwxyzABDEGJKLMNOPQRVWXYZ1234567890') assert h.decrypt('X50Yg6VPoAO4') == (7452, 2967, 21401) assert h.decrypt('GAbDdR') == (1, 2, 3) assert h.decrypt('5NMPD') == (60125, ) assert h.decrypt('yGya5') == (99, 25)
def test_single_number(self): h = Hashids() assert h.decrypt('rGAx') == (12345,) assert h.decrypt('yE') == (1,) assert h.decrypt('B8') == (22,) assert h.decrypt('7G9') == (333,) assert h.decrypt('zpz5') == (9999,)
def test_alphabet_with_two_standard_separators(self): h = Hashids( alphabet='abdegjklmnopqrvwxyzABDEGJKLMNOPQRVWXYZ1234567890uC') assert h.decrypt('GJNNmKYzbPBw') == (7452, 2967, 21401) assert h.decrypt('DQCXa4') == (1, 2, 3) assert h.decrypt('38V1D') == (60125, ) assert h.decrypt('373az') == (99, 25)
def test_single_number(self): h = Hashids() assert h.decrypt('j0gW') == (12345,) assert h.decrypt('jR') == (1,) assert h.decrypt('Lw') == (22,) assert h.decrypt('Z0E') == (333,) assert h.decrypt('w0rR') == (9999,)
def test_min_length(self): h = Hashids(min_length=25) assert h.decrypt('pO3K69b86jzc6krI416enr2B5') == ( 7452, 2967, 21401, ) assert h.decrypt('gyOwl4B97bo2fXhVaDR0Znjrq') == ( 1, 2, 3, ) assert h.decrypt('Nz7x3VXyMYerRmWeOBQn6LlRG') == (6097, ) assert h.decrypt('k91nqP3RBe3lKfDaLJrvy8XjV') == ( 99, 25, )
def id_from_hash(cls, hsh): hashids = Hashids(salt=settings.SECRET_KEY) ids = hashids.decrypt(hsh) if len(ids) > 0: return ids[0] return None
def test_all_parameters(self): h = Hashids('arbitrary salt', 16, 'abcdefghijklmnopqrstuvwxyz') assert h.decrypt('wygqxeunkatjgkrw') == ( 7452, 2967, 21401, ) assert h.decrypt('pnovxlaxuriowydb') == ( 1, 2, 3, ) assert h.decrypt('jkbgxljrjxmlaonp') == (60125, ) assert h.decrypt('erdjpwrgouoxlvbx') == ( 99, 25, )
class ID(object): def __init__(self): self.hashids = Hashids(salt='!@sasSD092f0Hu7&', min_length=6) def int2hash(self, id): return self.hashids.encrypt(long(id)) def hash2int(self, hash_id): return self.hashids.decrypt(hash_id)[0]
class ID(object): def __init__(self): self.hashids = Hashids(salt='!@sasSD092f0Hu7&', min_length=6) def int2hash(self, id): return self.hashids.encrypt(long(id)) def hash2int(self, hash_id): return self.hashids.decrypt(hash_id)[0]
def check_auth_token(token,userid): hashids = Hashids(salt=userid) dt = datetime.datetime.now() raw = hashids.decrypt(token) print raw print int(dt.strftime("%d"))+int(dt.strftime("%m"))+int(dt.strftime("%Y")) try: return raw[0] == int(dt.strftime("%d"))+int(dt.strftime("%m"))+int(dt.strftime("%Y")) except IndexError: return 2
def hash_to_id(short_id, salt=None, length=4, default=0): if salt is None: salt = settings.SECRET_KEY hashids = Hashids(salt=salt, min_length=length) decrypt = hashids.decrypt(short_id) if len(decrypt) == 0: real_id = default else: real_id = decrypt[0] return real_id
def hash_to_id(short_id, salt=None, length=4, default=0): if salt is None: salt = settings.SECRET_KEY hashids = Hashids(salt=salt, min_length=length) decrypt = hashids.decrypt(short_id) if len(decrypt) == 0: real_id = default else: real_id = decrypt[0] return real_id
def test_alphabet(self): h = Hashids( alphabet= '!"#%&\',-/0123456789:;<=>ABCDEFGHIJKLMNOPQRSTUVWXYZ_`abcdefghijklmnopqrstuvwxyz~' ) assert h.decrypt('_nJUNTVU3') == ( 2839, 12, 32, 5, ) assert h.decrypt('7xfYh2') == ( 1, 2, 3, ) assert h.decrypt('Z6R>') == (23832, ) assert h.decrypt('AYyIB') == ( 99, 25, )
def test_salt(self): h = Hashids(salt='Arbitrary string') assert h.decrypt('QWyf8yboH7KT2') == ( 683, 94108, 123, 5, ) assert h.decrypt('neHrCa') == ( 1, 2, 3, ) assert h.decrypt('LRCgf2') == ( 2, 4, 6, ) assert h.decrypt('JOMh1') == ( 99, 25, )
def test_multiple_numbers(self): h = Hashids() assert h.decrypt('vJvi7On9cXGtD') == ( 683, 94108, 123, 5, ) assert h.decrypt('o2fXhV') == ( 1, 2, 3, ) assert h.decrypt('xGhmsW') == ( 2, 4, 6, ) assert h.decrypt('3lKfD') == ( 99, 25, )
class HashidsPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField): def __init__(self, *args, **kwargs): salt = kwargs.pop('salt', settings.SECRET_KEY) min_length = kwargs.pop('min_length', settings.HASHIDS_MIN_LENGTH) self.hashids = Hashids(salt=salt, min_length=min_length) super(HashidsPrimaryKeyRelatedField, self).__init__(*args, **kwargs) def to_native(self, pk): return self.hashids.encrypt(pk) def from_native(self, data): pk = self.hashids.decrypt(data)[0] return super(HashidsPrimaryKeyRelatedField, self).from_native(pk)
def test_alphabet(self): h = Hashids(alphabet='!"#%&\',-/0123456789:;<=>ABCDEFGHIJKLMNOPQRSTUVWXYZ_`abcdefghijklmnopqrstuvwxyz~') assert h.decrypt('!%u#Y=%#v') == (2839, 12, 32, 5,) assert h.decrypt(':Y9c#2') == (1, 2, 3,) assert h.decrypt('cZKL') == (23832,) assert h.decrypt('aNCEI') == (99, 25,)
def join(request, hash): hasher = Hashids(settings.EXCHANGE_HASH_SALT) exchangeID = hasher.decrypt(hash)[0] exchange = models.Exchange.objects.get(id=exchangeID) user = request.user if exchange.closeDate < datetime.date.today(): messages.error(request, "That exchange has closed.") return redirect('index') registerForm = None if request.method == 'POST': participateForm = forms.ParticipationForm(request.POST) if user.is_anonymous(): registerForm = forms.RegisterForm(request.POST) else: participateForm = forms.ParticipationForm() if user.is_anonymous(): registerForm = forms.RegisterForm() if user.is_anonymous() and registerForm.is_valid(): username = registerForm.cleaned_data['username'] password = registerForm.cleaned_data['password'] email = registerForm.cleaned_data['email'] loginUser = authenticate(username=username, password=password) if not loginUser: if models.User.objects.filter(username=username).exists(): messages.error(request, "That username is taken.") else: user = models.User.objects.create_user(username, email, password) fullName = registerForm.cleaned_data['displayName'].split(' ', 1) if len(fullName) == 1: lastName = "" else: lastName = ' '.join(fullName[1:]) firstName = fullName[0] user.first_name = firstName user.last_name = lastName user.save() user = authenticate(username=username, password=password) login(request, user) messages.success(request, "An account was created for you.") else: messages.info(request, "You already have an account, so now you're logged in.") user = loginUser login(request, loginUser) if not user.is_anonymous(): participation = models.Participant.objects.filter(user=user, exchange=exchange) if participation.exists(): messages.info(request, "You're already signed up for that exchange, silly.") participation = participation[0] elif participateForm.is_valid(): participation = models.Participant.objects.create( user = user, exchange = exchange, address = participateForm.cleaned_data['address'], internationalOK = participateForm.cleaned_data['internationalOK'], suggestions = participateForm.cleaned_data['suggestions'] ) messages.success(request, "The contract has been sealed. You are a part of the exchange.") if participation: return redirect('index') return render(request, 'santa/join.html', {'exchange': exchange, 'registerForm': registerForm, 'participationForm': participateForm})
class notify(): def __init__(self, ): conf = config().token() self.hash_id = Hashids(salt=conf['hash'], min_length=int(conf['tamanho_token'])) def generate(self, user_id, email_monitor): self.key = randint(1000, 9999) filename = '{0}'.format(str(user_id)) pathname = os.path.abspath(os.path.join('tokens', filename)) save = '{0}:{1}:{2}'.format(str(self.key), str(user_id), email_monitor) f = open(os.path.abspath(os.path.join('tokens', filename)), 'w').write(save) key_hash = self.hash_id.encrypt(self.key) result_send = sendemail(email_monitor, key_hash) return result_send def verify(self, user_id, msg, leaks): db = banco() response = textos['monitor_ativado_err'] if len(msg.split(' ')) == 3: cmd, email1, key_hash = msg.split(' ') filename = '{0}'.format(str(user_id)) pathname_token = os.path.abspath(os.path.join('tokens', filename)) if os.path.exists(pathname_token): if checkemail(email1) != None: for code in open(pathname_token): key, user, email = code.split(':') #QUERY BANCO result_key_hash = self.hash_id.decrypt(key_hash) if result_key_hash: if user_id == int(user) and result_key_hash[ 0] == int(key) and email1 == email.strip(): response = textos['monitor_ativado'] db.users.insert(user_service_id=user_id, email=email1) user = db(db.users.user_service_id == user_id).select().first() for leak in leaks.check(email1): db.leaks.insert(leaksource=leak, user_id=user) db.commit() os.remove(pathname_token) break else: response = textos['codigo_invalido'] else: response = textos['email_invalido'] else: response = textos['codigo_invalido'] return response def delete(self, user_id, msg): db = banco() cmd, email = msg.split(' ') query = db((db.users.user_service_id == user_id) & (db.users.email == email)) user = query.select() if user: query.delete() response = textos['delete_ok'] db.commit() else: response = textos['delete_err'] return response def monitor(self, user_id, msg): db = banco() monitor = msg.split(' ') if len(monitor) == 2: email_monitor = monitor[1] check_email = checkemail(email_monitor) if check_email != None: user = db((db.users.user_service_id == user_id) & (db.users.email == email_monitor)).select().first() if not user: email_result = self.generate(user_id, email_monitor) if email_result: response = textos['email_monitor_ativado'].format( email_monitor) else: response = textos['email_monitor_err'].format( email_monitor) else: response = textos['email_ja_cadastrado'] else: response = textos['email_invalido'] return response
def test_min_length(self): h = Hashids(min_length=25) assert h.decrypt('4ARhAecbrrGh8K7FBBbi4nkhL') == (7452, 2967, 21401,) assert h.decrypt('IeRX9XtbpTkatKSAcXe4tALde') == (1, 2, 3,) assert h.decrypt('aULxKgxFpEi7prdcK7LFLz4Lk') == (6097,) assert h.decrypt('UrBa8pCqLTnq4CGTaMpC7Kj6x') == (99, 25,)
def test_multiple_numbers(self): h = Hashids() assert h.decrypt('vJvi7On9cXGtD') == (683, 94108, 123, 5,) assert h.decrypt('o2fXhV') == (1, 2, 3,) assert h.decrypt('xGhmsW') == (2, 4, 6,) assert h.decrypt('3lKfD') == (99, 25,)
def get_decrypt(hashid): hashids = Hashids(alphabet='abcdefghijklmnopqrstuvwxyz1234567890', salt=settings.HASHIDS_KEY) return hashids.decrypt(hashid)[0]
def test_only_one_valid(self): h = Hashids(min_length=6) assert h.decrypt(h.encrypt(1)[:-1] + '0') == ()
def test_all_parameters(self): h = Hashids('arbitrary salt', 16, 'abcdefghijklmnopqrstuvwxyz') assert h.decrypt('wygqxeunkatjgkrw') == (7452, 2967, 21401,) assert h.decrypt('pnovxlaxuriowydb') == (1, 2, 3,) assert h.decrypt('jkbgxljrjxmlaonp') == (60125,) assert h.decrypt('erdjpwrgouoxlvbx') == (99, 25,)
def test_alphabet_without_standard_separators(self): h = Hashids(alphabet='abdegjklmnopqrvwxyzABDEGJKLMNOPQRVWXYZ1234567890') assert h.decrypt('X50Yg6VPoAO4') == (7452, 2967, 21401) assert h.decrypt('GAbDdR') == (1, 2, 3) assert h.decrypt('5NMPD') == (60125,) assert h.decrypt('yGya5') == (99, 25)
def from_secret_id(cls, secret_id): hashids = Hashids(current_app.secret_key) unpacked_data = hashids.decrypt(secret_id) if len(unpacked_data) != 1: return return cls.query.get(unpacked_data[0])
def test_min_length(self): h = Hashids(min_length=25) assert h.decrypt('pO3K69b86jzc6krI416enr2B5') == (7452, 2967, 21401,) assert h.decrypt('gyOwl4B97bo2fXhVaDR0Znjrq') == (1, 2, 3,) assert h.decrypt('Nz7x3VXyMYerRmWeOBQn6LlRG') == (6097,) assert h.decrypt('k91nqP3RBe3lKfDaLJrvy8XjV') == (99, 25,)
def test_salt(self): h = Hashids(salt='Arbitrary string') assert h.decrypt('QWyf8yboH7KT2') == (683, 94108, 123, 5,) assert h.decrypt('neHrCa') == (1, 2, 3,) assert h.decrypt('LRCgf2') == (2, 4, 6,) assert h.decrypt('JOMh1') == (99, 25,)
def test_all_parameters(self): h = Hashids('arbitrary salt', 16, 'abcdefghijklmnopqrstuvwxyz') assert h.decrypt('mjpnilkonugzfjub') == (7452, 2967, 21401,) assert h.decrypt('nqyjbjpcfeymvfiq') == (1, 2, 3,) assert h.decrypt('vxwfjmrnfvtmpdow') == (60125,) assert h.decrypt('hsnymlyueozbnijs') == (99, 25,)
def test_multiple_numbers(self): h = Hashids() assert h.decrypt('6nph8p9duq8u9') == (683, 94108, 123, 5,) assert h.decrypt('katKSA') == (1, 2, 3,) assert h.decrypt('5jhof9') == (2, 4, 6,) assert h.decrypt('nq4CG') == (99, 25,)
from hashids import Hashids hashids = Hashids() hash_str = hashids.encrypt(123) print('Encrypt(123) -> %s' % hash_str) x = hashids.decrypt(hash_str) print('decrypt(%s) -> %d' % (hash_str, x[0]))
def test_alphabet_with_two_standard_separators(self): h = Hashids(alphabet='abdegjklmnopqrvwxyzABDEGJKLMNOPQRVWXYZ1234567890uC') assert h.decrypt('GJNNmKYzbPBw') == (7452, 2967, 21401) assert h.decrypt('DQCXa4') == (1, 2, 3) assert h.decrypt('38V1D') == (60125,) assert h.decrypt('373az') == (99, 25)
def test_alphabet(self): h = Hashids(alphabet='!"#%&\',-/0123456789:;<=>ABCDEFGHIJKLMNOPQRSTUVWXYZ_`abcdefghijklmnopqrstuvwxyz~') assert h.decrypt('_nJUNTVU3') == (2839, 12, 32, 5,) assert h.decrypt('7xfYh2') == (1, 2, 3,) assert h.decrypt('Z6R>') == (23832,) assert h.decrypt('AYyIB') == (99, 25,)
def test_salt(self): h = Hashids(salt='Arbitrary string') assert h.decrypt('q9khp7X9u6BuE') == (683, 94108, 123, 5,) assert h.decrypt('a7tLSG') == (1, 2, 3,) assert h.decrypt('Xbh4fp') == (2, 4, 6,) assert h.decrypt('K6nCz') == (99, 25,)