def test_base64(): allowed_s = string.ascii_letters + string.digits + '+/=' allowed_d = {} for c in allowed_s: allowed_d[c] = None isAllowed = allowed_d.has_key def checkEncoded(s): for c in s: assert isAllowed(c), s cases = [ '', 'x', '\x00', '\x01', '\x00' * 100, ''.join(map(chr, range(256))), ] for s in cases: b64 = oidutil.toBase64(s) checkEncoded(b64) s_prime = oidutil.fromBase64(b64) assert s_prime == s, (s, b64, s_prime) # Randomized test for _ in xrange(50): n = random.randrange(2048) s = ''.join(map(chr, map(lambda _: random.randrange(256), range(n)))) b64 = oidutil.toBase64(s) checkEncoded(b64) s_prime = oidutil.fromBase64(b64) assert s_prime == s, (s, b64, s_prime)
def getMessageSignature(self, message): """Return the signature of a message. If I am not a sign-all association, the message must have a signed list. @return: the signature, base64 encoded @rtype: str @raises ValueError: If there is no signed list and I am not a sign-all type of association. """ pairs = self._makePairs(message) return oidutil.toBase64(self.sign(pairs))
def serialize(self): """ Convert an association to KV form. @return: String in KV form suitable for deserialization by deserialize. @rtype: str """ data = { 'version': '2', 'handle': self.handle, 'secret': oidutil.toBase64(self.secret), 'issued': str(int(self.issued)), 'lifetime': str(int(self.lifetime)), 'assoc_type': self.assoc_type } assert len(data) == len(self.assoc_keys) pairs = [] for field_name in self.assoc_keys: pairs.append((field_name, data[field_name])) return kvform.seqToKV(pairs, strict=True)
def serialize(self): """ Convert an association to KV form. @return: String in KV form suitable for deserialization by deserialize. @rtype: str """ data = { 'version':'2', 'handle':self.handle, 'secret':oidutil.toBase64(self.secret), 'issued':str(int(self.issued)), 'lifetime':str(int(self.lifetime)), 'assoc_type':self.assoc_type } assert len(data) == len(self.assoc_keys) pairs = [] for field_name in self.assoc_keys: pairs.append((field_name, data[field_name])) return kvform.seqToKV(pairs, strict=True)
def longToBase64(l): return toBase64(longToBinary(l))
def _safe64(s): h64 = oidutil.toBase64(cryptutil.sha1(s)) h64 = h64.replace('+', '_') h64 = h64.replace('/', '.') h64 = h64.replace('=', '') return h64