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)
Exemple #2
0
def test_base64():
    allowed_s = string.ascii_letters + string.digits + '+/='
    allowed_d = {}
    for c in allowed_s:
        allowed_d[ord(c)] = None
    isAllowed = allowed_d.__contains__

    def checkEncoded(s):
        for c in s:
            assert isAllowed(c), s

    cases = [
        '',
        'x',
        '\x00',
        '\x01',
        '\x00' * 100,
        ''.join(map(chr, list(range(256)))),
    ]

    for s in cases:
        b64 = oidutil.toBase64(s)
        checkEncoded(b64)
        s_prime = oidutil.fromBase64(b64)
        assert str(s_prime, encoding="utf-8") == s, (s, b64, s_prime)

    # Randomized test
    for _ in range(50):
        n = random.randrange(2048)
        s = ''.join(map(chr, [random.randrange(256) for _ in range(n)]))
        b64 = oidutil.toBase64(s)
        checkEncoded(b64)
        s_prime = oidutil.fromBase64(b64)
        assert str(s_prime, encoding="utf-8") == s, (s, b64, s_prime)
Exemple #3
0
def test_base64():
    allowed_s = string.ascii_letters + string.digits + '+/='
    allowed_d = {}
    for c in allowed_s:
        allowed_d[ord(c)] = None
    isAllowed = allowed_d.__contains__

    def checkEncoded(s):
        for c in s:
            assert isAllowed(c), s

    cases = [
        '',
        'x',
        '\x00',
        '\x01',
        '\x00' * 100,
        ''.join(map(chr, list(range(256)))),
    ]

    for s in cases:
        b64 = oidutil.toBase64(s)
        checkEncoded(b64)
        s_prime = oidutil.fromBase64(b64)
        assert str(s_prime, encoding="utf-8") == s, (s, b64, s_prime)

    # Randomized test
    for _ in range(50):
        n = random.randrange(2048)
        s = ''.join(map(chr, [random.randrange(256) for _ in range(n)]))
        b64 = oidutil.toBase64(s)
        checkEncoded(b64)
        s_prime = oidutil.fromBase64(b64)
        assert str(s_prime, encoding="utf-8") == s, (s, b64, s_prime)
Exemple #4
0
    def deserialize(cls, assoc_s):
        """
        Parse an association as stored by serialize().

        inverse of serialize

        @param assoc_s: Association as serialized by serialize()
        @type assoc_s: bytes

        @return: instance of this class
        """
        pairs = kvform.kvToSeq(assoc_s, strict=True)
        keys = []
        values = []
        for k, v in pairs:
            keys.append(k)
            values.append(v)

        if keys != cls.assoc_keys:
            raise ValueError('Unexpected key values: %r', keys)

        version, handle, secret, issued, lifetime, assoc_type = values
        if version != '2':
            raise ValueError('Unknown version: %r' % version)
        issued = int(issued)
        lifetime = int(lifetime)
        secret = oidutil.fromBase64(secret)
        return cls(handle, secret, issued, lifetime, assoc_type)
    def deserialize(cls, assoc_s):
        """
        Parse an association as stored by serialize().

        inverse of serialize

        @param assoc_s: Association as serialized by serialize()
        @type assoc_s: bytes

        @return: instance of this class
        """
        pairs = kvform.kvToSeq(assoc_s, strict=True)
        keys = []
        values = []
        for k, v in pairs:
            keys.append(k)
            values.append(v)

        if keys != cls.assoc_keys:
            raise ValueError("Unexpected key values: %r", keys)

        version, handle, secret, issued, lifetime, assoc_type = values
        if version != "2":
            raise ValueError("Unknown version: %r" % version)
        issued = int(issued)
        lifetime = int(lifetime)
        secret = oidutil.fromBase64(secret)
        return cls(handle, secret, issued, lifetime, assoc_type)
    def deserialize(cls, assoc_s):
        """
        Parse an association as stored by serialize().

        inverse of serialize


        @param assoc_s: Association as serialized by serialize()
        @type assoc_s: six.text_type, six.binary_type is deprecated

        @return: instance of this class
        """
        pairs = kvform.kvToSeq(string_to_text(
            assoc_s,
            "Binary values for assoc_s are deprecated. Use text input instead."
        ),
                               strict=True)
        keys = []
        values = []
        for k, v in pairs:
            keys.append(k)
            values.append(v)

        if keys != cls.assoc_keys:
            raise ValueError('Unexpected key values: %r', keys)

        version, handle, secret, issued, lifetime, assoc_type = values
        if version != '2':
            raise ValueError('Unknown version: %r' % version)
        issued = int(issued)
        lifetime = int(lifetime)
        secret = oidutil.fromBase64(secret)
        return cls(handle, secret, issued, lifetime, assoc_type)
Exemple #7
0
 def _mkAssoc(self, doc):
     return association.Association(handle=doc['handle'],
                                    secret=oidutil.fromBase64(
                                        doc['secret']),
                                    issued=doc['issued'],
                                    lifetime=doc['lifetime'],
                                    assoc_type=doc['type'])
    def test_base64(self):
        allowed_s = string.ascii_letters + string.digits + '+/='
        allowed_d = {}
        for c in allowed_s:
            allowed_d[c] = None

        def checkEncoded(s):
            for c in s:
                self.assertIn(c, allowed_d, msg=s)

        cases = [
            b'',
            b'x',
            b'\x00',
            b'\x01',
            b'\x00' * 100,
        ]
        if six.PY2:
            cases.append(b''.join(chr(i) for i in range(256)))
        else:
            assert six.PY3
            cases.append(bytes(i for i in 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 range(50):
            n = random.randrange(2048)
            if six.PY2:
                s = b''.join(chr(random.randrange(256)) for i in range(n))
            else:
                assert six.PY3
                s = bytes(random.randrange(256) for i in range(n))
            b64 = oidutil.toBase64(s)
            checkEncoded(b64)
            s_prime = oidutil.fromBase64(b64)
            assert s_prime == s, (s, b64, s_prime)
Exemple #9
0
    def test_base64(self):
        allowed_s = string.ascii_letters + string.digits + '+/='
        allowed_d = {}
        for c in allowed_s:
            allowed_d[c] = None

        def checkEncoded(s):
            for c in s:
                self.assertIn(c, allowed_d, msg=s)

        cases = [
            b'',
            b'x',
            b'\x00',
            b'\x01',
            b'\x00' * 100,
        ]
        if six.PY2:
            cases.append(b''.join(chr(i) for i in range(256)))
        else:
            assert six.PY3
            cases.append(bytes(i for i in 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 range(50):
            n = random.randrange(2048)
            if six.PY2:
                s = b''.join(chr(random.randrange(256)) for i in range(n))
            else:
                assert six.PY3
                s = bytes(random.randrange(256) for i in range(n))
            b64 = oidutil.toBase64(s)
            checkEncoded(b64)
            s_prime = oidutil.fromBase64(b64)
            assert s_prime == s, (s, b64, s_prime)
Exemple #10
0
    def getAssociation(self, server_url, handle=None):
        iden = '%s-%s' % (server_url, handle)
        data = self.get_unique_data('association', iden)

        if len(data) < 1:
            return None

        datum = data[iden]
        assoc = Association(handle, oidutil.fromBase64(datum['secret']),
                            int(datum['issued']), int(datum['lifetime']),
                            datum['assoc_type'])

        if assoc.expiresIn == 0:
            self.del_unique_data('association', iden)
            return None

        return assoc
Exemple #11
0
    def getAssociation(self, server_url, handle=None):
        iden = '%s-%s' % (server_url, handle)
        data = self.get_unique_data('association', iden)

        if len(data) < 1:
            return None

        datum = data[iden]
        assoc = Association(handle,
                            oidutil.fromBase64(datum['secret']),
                            int(datum['issued']),
                            int(datum['lifetime']),
                            datum['assoc_type'])

        if assoc.expiresIn == 0:
            self.del_unique_data('association', iden)
            return None

        return assoc
Exemple #12
0
def base64ToLong(s):
    return binaryToLong(fromBase64(s))
Exemple #13
0
def base64ToLong(s):
    return binaryToLong(fromBase64(s))
Exemple #14
0
def base64ToLong(s):
    return bytes_to_int(fromBase64(s))
Exemple #15
0
 def extractSecret(self, response):
     return oidutil.fromBase64(response['mac_key'])
Exemple #16
0
 def extractSecret(self, response):
     spub = cryptutil.base64ToLong(response['dh_server_public'])
     enc_mac_key = oidutil.fromBase64(response['enc_mac_key'])
     return self.dh.xorSecret(spub, enc_mac_key)