def props_from_args(db, cl, args, itemid=None): """Construct a list of properties from the given arguments, and return them after validation.""" props = {} for arg in args: if isinstance(arg, Binary): arg = arg.data try : key, value = arg.split('=', 1) except ValueError : raise UsageError('argument "%s" not propname=value'%arg) key = us2s(key) value = us2s(value) if value: try: props[key] = hyperdb.rawToHyperdb(db, cl, itemid, key, value) except hyperdb.HyperdbValueError as message: raise UsageError(message) else: # If we're syncing a file the contents may not be None if key == 'content': props[key] = '' else: props[key] = None return props
def write_csv(klass, data): props = klass.getprops() if not os.path.exists('/tmp/imported'): os.mkdir('/tmp/imported') f = open('/tmp/imported/%s.csv'%klass.classname, 'w') writer = csv.writer(f, colon_separated) propnames = klass.export_propnames() propnames.append('is retired') writer.writerow(propnames) for entry in data: row = [] for name in propnames: if name == 'is retired': continue prop = props[name] if name in entry: if isinstance(prop, hyperdb.Date) or \ isinstance(prop, hyperdb.Interval): row.append(repr(entry[name].get_tuple())) elif isinstance(prop, hyperdb.Password): row.append(repr(str(entry[name]))) else: row.append(repr(entry[name])) elif isinstance(prop, hyperdb.Multilink): row.append('[]') elif name in ('creator', 'actor'): row.append("'1'") elif name in ('created', 'activity'): row.append(repr(today.get_tuple())) else: row.append('None') row.append(entry.get('is retired', False)) writer.writerow(row) if isinstance(klass, hyperdb.FileClass) and entry.get('content'): fname = klass.exportFilename('/tmp/imported/', entry['id']) support.ensureParentsExist(fname) c = open(fname, 'wb') if isinstance(entry['content'], bytes): c.write(entry['content']) else: c.write(s2b(us2s(entry['content']))) c.close() f.close() f = open('/tmp/imported/%s-journals.csv'%klass.classname, 'w') f.close()
def pbkdf2_unpack(pbkdf2): """ unpack pbkdf2 encrypted password into parts, assume it has format "{rounds}${salt}${digest} """ pbkdf2 = us2s(pbkdf2) try: rounds, salt, digest = pbkdf2.split("$") except ValueError: raise PasswordValueError("invalid PBKDF2 hash (wrong number of " "separators)") if rounds.startswith("0"): raise PasswordValueError("invalid PBKDF2 hash (zero-padded rounds)") try: rounds = int(rounds) except ValueError: raise PasswordValueError("invalid PBKDF2 hash (invalid rounds)") raw_salt = h64decode(salt) return rounds, salt, raw_salt, digest
def pbkdf2(password, salt, rounds, keylen): """pkcs#5 password-based key derivation v2.0 :arg password: passphrase to use to generate key (if unicode, converted to utf-8) :arg salt: salt bytes to use when generating key :param rounds: number of rounds to use to generate key :arg keylen: number of bytes to generate If hashlib supports pbkdf2, uses it's implementation as backend. :returns: raw bytes of generated key """ password = s2b(us2s(password)) if keylen > 40: #NOTE: pbkdf2 allows up to (2**31-1)*20 bytes, # but m2crypto has issues on some platforms above 40, # and such sizes aren't needed for a password hash anyways... raise ValueError("key length too large") if rounds < 1: raise ValueError("rounds must be positive number") return _pbkdf2(password, salt, rounds, keylen)