res += chr(ord(char) - adder)
return res
###############################################################################
top-level entry points
###############################################################################
def encode(pswd):
return stringify(do_encode(pswd)) # encrypt plus string encode
def decode(pswd):
return do_decode(unstringify(pswd))
In addition to encryption, this module also implements an encoding method for already
encrypted strings, which transforms them to and from printable characters. By default,
the encoding functions do nothing, and the system relies on straight URL or HTML
encoding of the encrypted string. An optional encoding scheme translates the encrypted
string to a string of ASCII code digits separated by dashes. Either encoding method
makes nonprintable characters in the encrypted string printable.
To illustrate, let’s test this module’s tools interactively. For this test, we set
forceReadablePassword to True. The top-level entry points encode and decode into
printable characters (for illustration purposes, this test reflects a Python 2.X installation
where PyCrypto is installed):
>>> from secret import *
using PyCrypto
>>> data = encode('spam@123+')
>>> data
'-47-248-2-170-107-242-175-18-227-249-53-130-14-140-163-107'
>>> decode(data)
'spam@123+'
But there are actually two steps to this—encryption and printable encoding:
>>> raw = do_encode('spam@123+')
>>> raw
'/\xf8\x02\xaak\xf2\xaf\x12\xe3\xf95\x82\x0e\x8c\xa3k'
>>> text = stringify(raw)
>>> text
'-47-248-2-170-107-242-175-18-227-249-53-130-14-140-163-107'
>>> len(raw), len(text)
(16, 58)
Here’s what the encoding looks like without the extra printable encoding:
>>> raw = do_encode('spam@123+')
>>> raw
'/\xf8\x02\xaak\xf2\xaf\x12\xe3\xf95\x82\x0e\x8c\xa3k'
>>> do_decode(raw)
'spam@123+'
Utility Modules | 1285