Пример #1
0
def DigestCalcResponse(
    HA1,
    pszNonce,
    pszNonceCount,
    pszCNonce,
    pszQop,
    pszMethod,
    pszDigestUri,
    pszHEntity,
):
    m = md5()
    m.update(pszMethod)
    m.update(":")
    m.update(pszDigestUri)
    if pszQop == "auth-int":
        m.update(":")
        m.update(pszHEntity)
    HA2 = m.digest().encode('hex')

    m = md5()
    m.update(HA1)
    m.update(":")
    m.update(pszNonce)
    m.update(":")
    if pszNonceCount and pszCNonce:  # pszQop:
        m.update(pszNonceCount)
        m.update(":")
        m.update(pszCNonce)
        m.update(":")
        m.update(pszQop)
        m.update(":")
    m.update(HA2)
    hash = m.digest().encode('hex')
    return hash
Пример #2
0
def DigestCalcResponse(
    HA1,
    pszNonce,
    pszNonceCount,
    pszCNonce,
    pszQop,
    pszMethod,
    pszDigestUri,
    pszHEntity,
):
    m = md5()
    m.update(pszMethod)
    m.update(":")
    m.update(pszDigestUri)
    if pszQop == "auth-int":
        m.update(":")
        m.update(pszHEntity)
    HA2 = m.digest().encode('hex')

    m = md5()
    m.update(HA1)
    m.update(":")
    m.update(pszNonce)
    m.update(":")
    if pszNonceCount and pszCNonce: # pszQop:
        m.update(pszNonceCount)
        m.update(":")
        m.update(pszCNonce)
        m.update(":")
        m.update(pszQop)
        m.update(":")
    m.update(HA2)
    hash = m.digest().encode('hex')
    return hash
Пример #3
0
    def test_md5(self):
        """
        L{hashreqs.md5} returns an object which can be used to compute an MD5
        hash as defined by U{RFC 1321<http://www.ietf.org/rfc/rfc1321.txt>}.
        """
        # Test the result using values from section A.5 of the RFC.
        self.assertEqual(md5().hexdigest(), "d41d8cd98f00b204e9800998ecf8427e")
        self.assertEqual(
            md5("a").hexdigest(), "0cc175b9c0f1b6a831c399e269772661")
        self.assertEqual(
            md5("abc").hexdigest(), "900150983cd24fb0d6963f7d28e17f72")
        self.assertEqual(
            md5("message digest").hexdigest(),
            "f96b697d7cb7938d525a2f31aaf161d0")
        self.assertEqual(
            md5("abcdefghijklmnopqrstuvwxyz").hexdigest(),
            "c3fcd3d76192e4007dfb496cca67e13b")
        self.assertEqual(
            md5("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
                "0123456789").hexdigest(), "d174ab98d277d9f5a5611c2c9f419d9f")
        self.assertEqual(
            md5("1234567890123456789012345678901234567890123456789012345678901"
                "2345678901234567890").hexdigest(),
            "57edf4a22be3c955ac49da2e2107b67a")

        # It should have digest and update methods, too.
        self.assertEqual(md5().digest().encode('hex'),
                         "d41d8cd98f00b204e9800998ecf8427e")
        hash = md5()
        hash.update("a")
        self.assertEqual(hash.digest().encode('hex'),
                         "0cc175b9c0f1b6a831c399e269772661")

        # Instances of it should have a digest_size attribute
        self.assertEqual(md5().digest_size, 16)
Пример #4
0
    def _unzipIterChunkyTest(self, compression, chunksize, lower, upper):
        """
        unzipIterChunky should unzip the given number of bytes per iteration.
        """
        junk = ' '.join([str(random.random()) for n in xrange(1000)])
        junkmd5 = md5(junk).hexdigest()

        tempdir = filepath.FilePath(self.mktemp())
        tempdir.makedirs()
        zfpath = tempdir.child('bigfile.zip').path
        self._makebigfile(zfpath, compression, junk)
        uziter = zipstream.unzipIterChunky(zfpath, tempdir.path,
                                           chunksize=chunksize)
        r = uziter.next()
        # test that the number of chunks is in the right ballpark;
        # this could theoretically be any number but statistically it
        # should always be in this range
        approx = lower < r < upper
        self.failUnless(approx)
        for r in uziter:
            pass
        self.assertEqual(r, 0)
        newmd5 = md5(
            tempdir.child("zipstreamjunk").open().read()).hexdigest()
        self.assertEqual(newmd5, junkmd5)
Пример #5
0
def _abstractPath(case):
    """
    Return a new, unique abstract namespace path to be listened on.
    """
    # Use the test cases's mktemp to get something unique, but also squash it
    # down to make sure it fits in the unix socket path limit (something around
    # 110 bytes).
    return md5(case.mktemp()).hexdigest()
Пример #6
0
    def test_md5(self):
        """
        L{hashreqs.md5} returns an object which can be used to compute an MD5
        hash as defined by U{RFC 1321<http://www.ietf.org/rfc/rfc1321.txt>}.
        """
        # Test the result using values from section A.5 of the RFC.
        self.assertEqual(
            md5().hexdigest(), "d41d8cd98f00b204e9800998ecf8427e")
        self.assertEqual(
            md5("a").hexdigest(), "0cc175b9c0f1b6a831c399e269772661")
        self.assertEqual(
            md5("abc").hexdigest(), "900150983cd24fb0d6963f7d28e17f72")
        self.assertEqual(
            md5("message digest").hexdigest(),
            "f96b697d7cb7938d525a2f31aaf161d0")
        self.assertEqual(
            md5("abcdefghijklmnopqrstuvwxyz").hexdigest(),
            "c3fcd3d76192e4007dfb496cca67e13b")
        self.assertEqual(
            md5("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
                "0123456789").hexdigest(),
            "d174ab98d277d9f5a5611c2c9f419d9f")
        self.assertEqual(
            md5("1234567890123456789012345678901234567890123456789012345678901"
                "2345678901234567890").hexdigest(),
            "57edf4a22be3c955ac49da2e2107b67a")

        # It should have digest and update methods, too.
        self.assertEqual(
            md5().digest().encode('hex'),
            "d41d8cd98f00b204e9800998ecf8427e")
        hash = md5()
        hash.update("a")
        self.assertEqual(
            hash.digest().encode('hex'),
            "0cc175b9c0f1b6a831c399e269772661")

        # Instances of it should have a digest_size attribute
        self.assertEqual(md5().digest_size, 16)
Пример #7
0
def DigestCalcHA1(
    pszAlg,
    pszUserName,
    pszRealm,
    pszPassword,
    pszNonce,
    pszCNonce,
):
    m = md5()
    m.update(pszUserName)
    m.update(":")
    m.update(pszRealm)
    m.update(":")
    m.update(pszPassword)
    HA1 = m.digest()
    if pszAlg == "md5-sess":
        m = md5()
        m.update(HA1)
        m.update(":")
        m.update(pszNonce)
        m.update(":")
        m.update(pszCNonce)
        HA1 = m.digest()
    return HA1.encode('hex')
Пример #8
0
def DigestCalcHA1(
    pszAlg,
    pszUserName,
    pszRealm,
    pszPassword,
    pszNonce,
    pszCNonce,
):
    m = md5()
    m.update(pszUserName)
    m.update(":")
    m.update(pszRealm)
    m.update(":")
    m.update(pszPassword)
    HA1 = m.digest()
    if pszAlg == "md5-sess":
        m = md5()
        m.update(HA1)
        m.update(":")
        m.update(pszNonce)
        m.update(":")
        m.update(pszCNonce)
        HA1 = m.digest()
    return HA1.encode('hex')
Пример #9
0
    def _makeContext(self):
        ctx = SSL.Context(self.method)

        if self.certificate is not None and self.privateKey is not None:
            ctx.use_certificate(self.certificate)
            ctx.use_privatekey(self.privateKey)
            # Sanity check
            ctx.check_privatekey()

        verifyFlags = SSL.VERIFY_NONE
        if self.verify:
            verifyFlags = SSL.VERIFY_PEER
            if self.requireCertificate:
                verifyFlags |= SSL.VERIFY_FAIL_IF_NO_PEER_CERT
            if self.verifyOnce:
                verifyFlags |= SSL.VERIFY_CLIENT_ONCE
            if self.caCerts:
                store = ctx.get_cert_store()
                for cert in self.caCerts:
                    store.add_cert(cert)

        # It'd be nice if pyOpenSSL let us pass None here for this behavior (as
        # the underlying OpenSSL API call allows NULL to be passed).  It
        # doesn't, so we'll supply a function which does the same thing.
        def _verifyCallback(conn, cert, errno, depth, preverify_ok):
            return preverify_ok

        ctx.set_verify(verifyFlags, _verifyCallback)

        if self.verifyDepth is not None:
            ctx.set_verify_depth(self.verifyDepth)

        if self.enableSingleUseKeys:
            ctx.set_options(SSL.OP_SINGLE_DH_USE)

        if self.fixBrokenPeers:
            ctx.set_options(self._OP_ALL)

        if self.enableSessions:
            sessionName = md5(
                "%s-%d" %
                (reflect.qual(self.__class__), _sessionCounter())).hexdigest()
            ctx.set_session_id(sessionName)

        if not self.enableSessionTickets:
            ctx.set_options(self._OP_NO_TICKET)

        return ctx
Пример #10
0
    def _makeContext(self):
        ctx = SSL.Context(self.method)

        if self.certificate is not None and self.privateKey is not None:
            ctx.use_certificate(self.certificate)
            ctx.use_privatekey(self.privateKey)
            # Sanity check
            ctx.check_privatekey()

        verifyFlags = SSL.VERIFY_NONE
        if self.verify:
            verifyFlags = SSL.VERIFY_PEER
            if self.requireCertificate:
                verifyFlags |= SSL.VERIFY_FAIL_IF_NO_PEER_CERT
            if self.verifyOnce:
                verifyFlags |= SSL.VERIFY_CLIENT_ONCE
            if self.caCerts:
                store = ctx.get_cert_store()
                for cert in self.caCerts:
                    store.add_cert(cert)

        # It'd be nice if pyOpenSSL let us pass None here for this behavior (as
        # the underlying OpenSSL API call allows NULL to be passed).  It
        # doesn't, so we'll supply a function which does the same thing.
        def _verifyCallback(conn, cert, errno, depth, preverify_ok):
            return preverify_ok
        ctx.set_verify(verifyFlags, _verifyCallback)

        if self.verifyDepth is not None:
            ctx.set_verify_depth(self.verifyDepth)

        if self.enableSingleUseKeys:
            ctx.set_options(SSL.OP_SINGLE_DH_USE)

        if self.fixBrokenPeers:
            ctx.set_options(self._OP_ALL)

        if self.enableSessions:
            sessionName = md5("%s-%d" % (reflect.qual(self.__class__), _sessionCounter())).hexdigest()
            ctx.set_session_id(sessionName)

        if not self.enableSessionTickets:
            ctx.set_options(self._OP_NO_TICKET)

        return ctx
Пример #11
0
    def test_challenge(self):
        """
        L{NotificationClient} responds to a I{CHL} message by sending a I{QRY}
        back which included a hash based on the parameters of the I{CHL}.
        """
        transport = StringTransport()
        self.client.makeConnection(transport)
        transport.clear()

        challenge = "15570131571988941333"
        self.client.dataReceived('CHL 0 ' + challenge + '\r\n')
        # md5 of the challenge and a magic string defined by the protocol
        response = "8f2f5a91b72102cd28355e9fc9000d6e"
        # Sanity check - the response is what the comment above says it is.
        self.assertEquals(response,
                          md5(challenge + "Q1P7W2E4J9R8U3S5").hexdigest())
        self.assertEquals(
            transport.value(),
            # 2 is the next transaction identifier.  32 is the length of the
            # response.
            "QRY 2 [email protected] 32\r\n" + response)
Пример #12
0
    def test_challenge(self):
        """
        L{NotificationClient} responds to a I{CHL} message by sending a I{QRY}
        back which included a hash based on the parameters of the I{CHL}.
        """
        transport = StringTransport()
        self.client.makeConnection(transport)
        transport.clear()

        challenge = "15570131571988941333"
        self.client.dataReceived('CHL 0 ' + challenge + '\r\n')
        # md5 of the challenge and a magic string defined by the protocol
        response = "8f2f5a91b72102cd28355e9fc9000d6e"
        # Sanity check - the response is what the comment above says it is.
        self.assertEquals(
            response, md5(challenge + "Q1P7W2E4J9R8U3S5").hexdigest())
        self.assertEquals(
            transport.value(),
            # 2 is the next transaction identifier.  32 is the length of the
            # response.
            "QRY 2 [email protected] 32\r\n" + response)
Пример #13
0
 def keyHash(self):
     """
     MD5 hex digest of signature on an empty certificate request with this
     key.
     """
     return md5(self._emptyReq).hexdigest()
Пример #14
0
 def _gen_nonce(self):
     return md5("%s:%s:%s" % (str(random.random()), str(
         time.gmtime()), str(os.getpid()))).hexdigest()
Пример #15
0
def encryptPasswordMD5(password, key):
    m = md5()
    m.update(key)
    m.update(md5(password).digest())
    m.update("AOL Instant Messenger (SM)")
    return m.digest()
Пример #16
0
 def _gen_nonce(self):
     return md5("%s:%s:%s" % (str(random.random()) , str(time.gmtime()),str(os.getpid()))).hexdigest()
Пример #17
0
 def H(s):
     return md5(s).digest()
Пример #18
0
 def keyHash(self):
     """
     MD5 hex digest of signature on an empty certificate request with this
     key.
     """
     return md5(self._emptyReq).hexdigest()
Пример #19
0
def _encrypt(passphrase, data):
    from Crypto.Cipher import AES as cipher
    leftover = len(data) % cipher.block_size
    if leftover:
        data += ' '*(cipher.block_size - leftover)
    return cipher.new(md5(passphrase).digest()[:16]).encrypt(data)
Пример #20
0
 def H(s):
     return md5(s).digest()
Пример #21
0
def encryptPasswordMD5(password,key):
    m=md5()
    m.update(key)
    m.update(md5(password).digest())
    m.update("AOL Instant Messenger (SM)")
    return m.digest()
Пример #22
0
def _decrypt(passphrase, data):
    from Crypto.Cipher import AES
    return AES.new(md5(passphrase).digest()[:16]).decrypt(data)