def keyhash(key, code): """ Generate a hash of the activation code using the registration key @param key: the registration key @param code: the activation code @returns: the hash as string """ crypt = CRYPT(key=key, digest_alg="sha512", salt=None) return str(crypt(code.upper())[0])
def get_masterkey(cls, keyhash, token): """ Get the master key matching a keyhash Args: keyhash: the key hash from the Authorization header token: the master key auth token Returns: The master key Row """ # Get all valid master keys table = current.s3db.auth_masterkey query = (table.deleted == False) & \ (table.name != None) & \ (table.name != "") & \ (table.user_id != None) rows = current.db(query).select( table.id, table.uuid, table.name, table.user_id, ) # Get the app key app_key = current.deployment_settings.get_auth_masterkey_app_key() # Format keyhash formatted = "%s$%s$%s" % (DIGEST_ALG, token, keyhash) # Find match masterkey = None encrypt = CRYPT(digest_alg=DIGEST_ALG, salt=token) for row in rows: key = row.name if app_key: key = "%s:%s" % (key, app_key) if encrypt(key)[0] == formatted: masterkey = row break return masterkey