コード例 #1
0
ファイル: key.py プロジェクト: SamPenrose/id-keywrapping-demo
    from hashlib import md5 as md5_new
except ImportError:
    # for Python < 2.5
    from md5 import new as md5_new

__all__ = 'gen_salt', 'passphrase_to_salted_key_and_iv'

__doc__ = '''Tools related to key derivation service

See RFC2898 PKCS #5: Password-Based Cryptography Specification Version 2.0'''

md5 = lambda x: md5_new(x).digest()

def gen_salt(saltlen=8):
    return ''.join(map(lambda x: chr(randrange(256)), xrange(saltlen)))

def passphrase_to_salted_key_and_iv(passphrase, salt='', klen=16, ivlen=8):
    dklen = klen + ivlen
    dk = ''
    d = ''
    while len(dk) < dklen:
        d = md5(d + passphrase + salt)
        dk += d
    return (dk[:klen], dk[klen:dklen])

if __name__ == '__main__':
    from testutil import ok, qrepr
    a = '\x09\x8f\x6b\xcd\x46\x21\xd3\x73\xca\xde\x4e\x83\x26\x27\xb4\xf6'
    b = md5('test')
    print qrepr(b), ok(a == b)
コード例 #2
0
ファイル: aes.py プロジェクト: SamPenrose/id-keywrapping-demo
        ),
        ('\x8e\x73\xb0\xf7\xda\x0e\x64\x52\xc8\x10\xf3\x2b\x80\x90\x79\xe5\x62\xf8\xea\xd2\x52\x2c\x6b\x7b',
         (('\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a',
           '\xbd\x33\x4f\x1d\x6e\x45\xf2\x5f\xf7\x12\xa2\x14\x57\x1f\xa5\xcc'),
          ('\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51',
           '\x97\x41\x04\x84\x6d\x0a\xd3\xad\x77\x34\xec\xb3\xec\xee\x4e\xef'),
          ('\x30\xc8\x1c\x46\xa3\x5c\xe4\x11\xe5\xfb\xc1\x19\x1a\x0a\x52\xef',
           '\xef\x7a\xfd\x22\x70\xe2\xe6\x0a\xdc\xe0\xba\x2f\xac\xe6\x44\x4e'),
          ('\xf6\x9f\x24\x45\xdf\x4f\x9b\x17\xad\x2b\x41\x7b\xe6\x6c\x37\x10',
           '\x9a\x4b\x41\xba\x73\x8d\x6c\x72\xfb\x16\x69\x16\x03\xc1\x8e\x0e'),
         )
        ),
        ('\x60\x3d\xeb\x10\x15\xca\x71\xbe\x2b\x73\xae\xf0\x85\x7d\x77\x81\x1f\x35\x2c\x07\x3b\x61\x08\xd7\x2d\x98\x10\xa3\x09\x14\xdf\xf4',
         (('\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a',
           '\xf3\xee\xd1\xbd\xb5\xd2\xa0\x3c\x06\x4b\x5a\x7e\x3d\xb1\x81\xf8'),
          ('\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51',
           '\x59\x1c\xcb\x10\xd4\x10\xed\x26\xdc\x5b\xa7\x4a\x31\x36\x28\x70'),
          ('\x30\xc8\x1c\x46\xa3\x5c\xe4\x11\xe5\xfb\xc1\x19\x1a\x0a\x52\xef',
           '\xb6\xed\x21\xb9\x9c\xa6\xf4\xf9\xf1\x53\xe7\xb1\xbe\xaf\xed\x1d'),
          ('\xf6\x9f\x24\x45\xdf\x4f\x9b\x17\xad\x2b\x41\x7b\xe6\x6c\x37\x10',
           '\x23\x30\x4b\x7a\x39\xf9\xf3\xff\x06\x7d\x8d\x8f\x9e\x24\xec\xc7'),
         )
        )):
        print 'key_len = %d bit' % (len(key) * 8)
        c = aes(key)
        for text, cryp in text_cryp:
            cc = c.enc(text)
            print qrepr(cryp), ok(cc == cryp)
            tt = c.dec(cc)
            print qrepr(tt), ok(tt == text)
コード例 #3
0
ファイル: bf.py プロジェクト: SamPenrose/id-keywrapping-demo
     ('\x00\x00\x00\x00\x00\x00\x00\x00',
      '\xff\xff\xff\xff\xff\xff\xff\xff',
      '\x01\x49\x33\xe0\xcd\xaf\xf6\xe4'),
     ('\xff\xff\xff\xff\xff\xff\xff\xff',
      '\x00\x00\x00\x00\x00\x00\x00\x00',
      '\xf2\x1e\x9a\x77\xb7\x1c\x49\xbc'),
     ('\x01\x23\x45\x67\x89\xab\xcd\xef',
      '\x00\x00\x00\x00\x00\x00\x00\x00',
      '\x24\x59\x46\x88\x57\x54\x36\x9a'),
     ('\xfe\xdc\xba\x98\x76\x54\x32\x10',
      '\xff\xff\xff\xff\xff\xff\xff\xff',
      '\x6b\x5c\x5a\x9c\x5d\x9e\x0a\x5a'),):
     c = blowfish(k)
     ee = c.enc(t)
     tt = c.dec(ee)
     print qrepr(e), qrepr(ee), ok(e == ee)
     print qrepr(t), qrepr(tt), ok(t == tt)
 # Test keys and low-level interface
 kk = '\xf0\xe1\xd2\xc3\xb4\xa5\x96\x87\x78\x69\x5a\x4b\x3c\x2d\x1e\x0f\x00\x11\x22\x33\x44\x55\x66\x77'
 p, q = text_to_pair('\xfe\xdc\xba\x98\x76\x54\x32\x10')
 for l, e in zip(xrange(1, 25), (
     '\xf9\xad\x59\x7c\x49\xdb\x00\x5e',
     '\xe9\x1d\x21\xc1\xd9\x61\xa6\xd6',
     '\xe9\xc2\xb7\x0a\x1b\xc6\x5c\xf3',
     '\xbe\x1e\x63\x94\x08\x64\x0f\x05',
     '\xb3\x9e\x44\x48\x1b\xdb\x1e\x6e',
     '\x94\x57\xaa\x83\xb1\x92\x8c\x0d',
     '\x8b\xb7\x70\x32\xf9\x60\x62\x9d',
     '\xe8\x7a\x24\x4e\x2c\xc8\x5e\x82',
     '\x15\x75\x0e\x7a\x4f\x4e\xc5\x77',
     '\x12\x2b\xa7\x0b\x3a\xb6\x4a\xe0',