Skip to content

Instantly share code, notes, and snippets.

@coleifer
Created April 13, 2013 05:05
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save coleifer/875a826fc765dfbd853b to your computer and use it in GitHub Desktop.
Save coleifer/875a826fc765dfbd853b to your computer and use it in GitHub Desktop.
from hashlib import sha256
def get_hexdigest(salt, plaintext):
return sha256(salt + plaintext).hexdigest()
SECRET_KEY = 's3cr3t'
def make_password(plaintext, service):
salt = get_hexdigest(SECRET_KEY, service)[:20]
hsh = get_hexdigest(salt, plaintext)
return ''.join((salt, hsh))
ALPHABET = ('abcdefghijklmnopqrstuvwxyz'
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
'0123456789!@#$%^&*()-_')
def password(plaintext, service, length=10):
raw_hexdigest = make_password(plaintext, service)
# Convert the hexdigest into decimal
num = int(raw_hexdigest, 16)
# What base will we convert `num` into?
num_chars = len(ALPHABET)
# Build up the new password one "digit" at a time,
# up to a certain length
chars = []
while len(chars) < length:
num, idx = divmod(num, num_chars)
chars.append(ALPHABET[idx])
return ''.join(chars)
from peewee import *
db = SqliteDatabase('accounts.db')
class Service(Model):
name = CharField()
length = IntegerField(default=8)
symbols = BooleanField(default=True)
alphabet = CharField(default='')
class Meta:
database = db
def get_alphabet(self):
if self.alphabet:
return self.alphabet
alpha = ('abcdefghijklmnopqrstuvwxyz'
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
'0123456789')
if self.symbols:
alpha += '!@#$%^&*()-_'
return alpha
def password(self, plaintext):
return password(plaintext, self.name, self.length)
@classmethod
def search(cls, q):
return cls.select().where(cls.name ** ('%%%s%%' % q))
db.create_table(Service, True)
@Gr0d3N
Copy link

Gr0d3N commented Dec 5, 2015

This is awesome, thank you so much man!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment