<s> import codecs <EOL> import hashlib <EOL> import hmac as python_hmac <EOL> import math <EOL> import os <EOL> from six import text_type <EOL> from six . moves import xrange <EOL> def hmac_digest ( key , content ) : <EOL> return python_hmac . new ( <EOL> key . encode ( "<STR_LIT:ascii>" ) , <EOL> content . encode ( "<STR_LIT:utf-8>" ) , <EOL> hashlib . sha256 <EOL> ) . hexdigest ( ) <EOL> def get_hawk_credentials ( token = None ) : <EOL> if token is None : <EOL> token = os . urandom ( <NUM_LIT:32> ) <EOL> else : <EOL> token = codecs . decode ( token , "<STR_LIT>" ) <EOL> keyInfo = '<STR_LIT>' <EOL> keyMaterial = HKDF ( token , "<STR_LIT>" , keyInfo , <NUM_LIT:32> * <NUM_LIT:2> ) <EOL> token = codecs . encode ( token , "<STR_LIT>" ) . decode ( "<STR_LIT:utf-8>" ) <EOL> return token , { <EOL> '<STR_LIT:id>' : codecs . encode ( keyMaterial [ : <NUM_LIT:32> ] , "<STR_LIT>" ) . decode ( "<STR_LIT:utf-8>" ) , <EOL> '<STR_LIT:key>' : codecs . encode ( keyMaterial [ <NUM_LIT:32> : <NUM_LIT:64> ] , "<STR_LIT>" ) . decode ( "<STR_LIT:utf-8>" ) , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def HKDF_extract ( salt , IKM , hashmod = hashlib . sha256 ) : <EOL> """<STR_LIT>""" <EOL> if salt is None : <EOL> salt = b"<STR_LIT:\x00>" * hashmod ( ) . digest_size <EOL> if isinstance ( salt , text_type ) : <EOL> salt = salt . encode ( "<STR_LIT:utf-8>" ) <EOL> return python_hmac . new ( salt , IKM , hashmod ) . digest ( ) <EOL> def HKDF_expand ( PRK , info , L , hashmod = hashlib . sha256 ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( info , text_type ) : <EOL> info = info . encode ( "<STR_LIT:utf-8>" ) <EOL> digest_size = hashmod ( ) . digest_size <EOL> N = int ( math . ceil ( L * <NUM_LIT:1.0> / digest_size ) ) <EOL> assert N <= <NUM_LIT:255> <EOL> T = b"<STR_LIT>" <EOL> output = [ ] <EOL> for i in xrange ( <NUM_LIT:1> , N + <NUM_LIT:1> ) : <EOL> data = T + info + chr ( i ) . encode ( "<STR_LIT:utf-8>" ) <EOL> T = python_hmac . new ( PRK , data , hashmod ) . digest ( ) <EOL> output . append ( T ) <EOL> return b"<STR_LIT>" . join ( output ) [ : L ] <EOL> def HKDF ( secret , salt , info , size , hashmod = hashlib . sha256 ) : <EOL> """<STR_LIT>""" <EOL> PRK = HKDF_extract ( salt , secret , hashmod ) <EOL> return HKDF_expand ( PRK , info , size , hashmod ) </s>
