# HG changeset patch
# User Pascal Volk <user@localhost.localdomain.org>
# Date 1392592050 0
# Node ID 77561c118f422da20aadc49d0b68eebd95ccc593
# Parent  d3a246067e8f981ed3d1c906603206ea172b3237
VMM/password: Added function extract_scheme().

diff -r d3a246067e8f -r 77561c118f42 VirtualMailManager/password.py
--- a/VirtualMailManager/password.py	Sat Feb 15 23:38:27 2014 +0000
+++ b/VirtualMailManager/password.py	Sun Feb 16 23:07:30 2014 +0000
@@ -13,9 +13,11 @@
         random_password = randompw()
         scheme, encoding = verify_scheme(scheme)
         schemes, encodings = list_schemes()
+        scheme = extract_scheme(hashed_password)
 """
 
 import hashlib
+import re
 
 from base64 import b64encode
 from binascii import b2a_hex
@@ -309,6 +311,17 @@
 }
 
 
+def extract_scheme(password_hash):
+    """Returns the extracted password scheme from *password_hash*.
+
+    If the scheme couldn't be extracted, **None** will be returned.
+    """
+    scheme = re.match(r'^\{([^\}]{3,37})\}', password_hash)
+    if scheme:
+        return scheme.groups()[0]
+    return scheme
+
+
 def list_schemes():
     """Returns the tuple (schemes, encodings).