chiark / gitweb /
pwsafe, catacomb/pwsafe.py: Push database creation into module.
authorMark Wooding <mdw@distorted.org.uk>
Sat, 24 May 2014 13:00:03 +0000 (14:00 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Thu, 14 Aug 2014 09:53:36 +0000 (10:53 +0100)
It didn't even work where it was because `_wrapstr' wasn't available.
This code is now functional again.

catacomb/pwsafe.py
pwsafe

index da311c7cfac89f9b81e715be5a16fcd5a1452618..d0d1a35b027e131366fe75c34d536e79de5f46b8 100644 (file)
@@ -283,6 +283,35 @@ class PW (object):
     me.k = Crypto(c, h, m, me.ck, me.mk)
     me.magic = me.k.decrypt(me.db['magic'])
 
+  @classmethod
+  def create(cls, file, c, h, m, tag):
+    """
+    Create and initialize a new, empty, database FILE.
+
+    We want a GCipher subclass C, a GHash subclass H, and a GMAC subclass M;
+    and a Pixie passphrase TAG.
+
+    This doesn't return a working object: it just creates the database file
+    and gets out of the way.
+    """
+
+    ## Set up the cryptography.
+    pp = _C.ppread(tag, _C.PMODE_VERIFY)
+    ppk = PPK(pp, c, h, m)
+    ck = _C.rand.block(c.keysz.default)
+    mk = _C.rand.block(c.keysz.default)
+    k = Crypto(c, h, m, ck, mk)
+
+    ## Set up and initialize the database.
+    db = _G.open(file, 'n', 0600)
+    db['tag'] = tag
+    db['salt'] = ppk.salt
+    db['cipher'] = c.name
+    db['hash'] = h.name
+    db['mac'] = m.name
+    db['key'] = ppk.encrypt(_wrapstr(ck) + _wrapstr(mk))
+    db['magic'] = k.encrypt(_C.rand.block(h.hashsz))
+
   def keyxform(me, key):
     """
     Transform the KEY (actually a password tag) into a GDBM record key.
diff --git a/pwsafe b/pwsafe
index a5d15eac6cd78c53444982bc78b5b47660722304..91684c75faec4053cb81a954460f0c5f72d4ed24 100644 (file)
--- a/pwsafe
+++ b/pwsafe
@@ -105,26 +105,9 @@ def cmd_create(av):
   else:
     tag = 'pwsafe'
 
-  ## Choose a passphrase, and generate master keys.
-  pp = C.ppread(tag, C.PMODE_VERIFY)
-  if not mac: mac = hash + '-hmac'
-  c = C.gcciphers[cipher]
-  h = C.gchashes[hash]
-  m = C.gcmacs[mac]
-  ppk = PW.PPK(pp, c, h, m)
-  ck = C.rand.block(c.keysz.default)
-  mk = C.rand.block(m.keysz.default)
-  k = Crypto(c, h, m, ck, mk)
-
-  ## Set up the database, storing the basic information we need.
-  db = G.open(file, 'n', 0600)
-  db['tag'] = tag
-  db['salt'] = ppk.salt
-  db['cipher'] = cipher
-  db['hash'] = hash
-  db['mac'] = mac
-  db['key'] = ppk.encrypt(wrapstr(ck) + wrapstr(mk))
-  db['magic'] = k.encrypt(C.rand.block(h.hashsz))
+  ## Set up the database.
+  if mac is None: mac = hash + '-hmac'
+  PW.create(file, C.gcciphers[cipher], C.gchashes[hash], C.gcmacs[mac], tag)
 
 def cmd_changepp(av):
   if len(av) != 0: