Python Crypto.Hash.HMAC Examples

The following are 4 code examples of Crypto.Hash.HMAC(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module Crypto.Hash , or try the search function .
Example #1
Source File: mycrypto.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def expand( self ):
        """
        Return the expanded output key material.

        The output key material is calculated based on the given PRK, info and
        L.
        """

        tmp = ""

        # Prevent the accidental re-use of output keying material.
        if len(self.T) > 0:
            raise base.PluggableTransportError("HKDF-SHA256 OKM must not "
                                               "be re-used by application.")

        while self.length > len(self.T):
            tmp = Crypto.Hash.HMAC.new(self.prk, tmp + self.info +
                                       chr(self.ctr),
                                       Crypto.Hash.SHA256).digest()
            self.T += tmp
            self.ctr += 1

        return self.T[:self.length] 
Example #2
Source File: test_scramblesuit.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def extract( self, salt, ikm ):
        return Crypto.Hash.HMAC.new(salt, ikm, Crypto.Hash.SHA256).digest() 
Example #3
Source File: mycrypto.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def HMAC_SHA256_128( key, msg ):
    """
    Return the HMAC-SHA256-128 of the given `msg' authenticated by `key'.
    """

    assert(len(key) >= const.SHARED_SECRET_LENGTH)

    h = Crypto.Hash.HMAC.new(key, msg, Crypto.Hash.SHA256)

    # Return HMAC truncated to 128 out of 256 bits.
    return h.digest()[:16] 
Example #4
Source File: test_scramblesuit.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test3_invalidHMAC( self ):
        # Make the HMAC invalid.
        handshake = self.udh.createHandshake()
        if handshake[-1] != 'a':
            handshake = handshake[:-1] + 'a'
        else:
            handshake = handshake[:-1] + 'b'

        buf = obfs_buf.Buffer(handshake)

        self.failIf(self.udh.receivePublicKey(buf, lambda x: x) == True)