chiark / gitweb /
catacomb/__init__.py: Fix up cipher etc. names better.
authorMark Wooding <mdw@distorted.org.uk>
Thu, 11 May 2017 09:42:15 +0000 (10:42 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 13 May 2017 15:50:39 +0000 (16:50 +0100)
Now `sha512/256', for example, will have the right name.

catacomb/__init__.py

index 120ef111d967e3bc368daf9584a1ff7b285b06fd..f79b4d29b210c56129138a0c08607dff637d5bca 100644 (file)
@@ -34,6 +34,20 @@ from sys import argv as _argv
 ## For the benefit of the default keyreporter, we need the program na,e.
 _base._ego(_argv[0])
 
+## How to fix a name back into the right identifier.  Alas, the rules are not
+## consistent.
+def _fixname(name):
+
+  ## Hyphens consistently become underscores.
+  name = name.replace('-', '_')
+
+  ## But slashes might become underscores or just vanish.
+  if name.startswith('salsa20'): name = name.translate(None, '/')
+  else: name = name.replace('/', '_')
+
+  ## Done.
+  return name
+
 ## Initialize the module.  Drag in the static methods of the various
 ## classes; create names for the various known crypto algorithms.
 def _init():
@@ -56,9 +70,9 @@ def _init():
         setattr(c, j[plen:], classmethod(b[j]))
   for i in [gcciphers, gchashes, gcmacs, gcprps]:
     for c in i.itervalues():
-      d[c.name.replace('-', '_').translate(None, '/')] = c
+      d[_fixname(c.name)] = c
   for c in gccrands.itervalues():
-    d[c.name.replace('-', '_').translate(None, '/') + 'rand'] = c
+    d[_fixname(c.name + 'rand')] = c
 _init()
 
 ## A handy function for our work: add the methods of a named class to an