Browse Source

added hmac_sha1 shortuct function

pull/41/head
Rossen Georgiev 9 years ago
parent
commit
1543a7f263
  1. 20
      steam/core/crypto.py

20
steam/core/crypto.py

@ -53,13 +53,8 @@ def symmetric_encrypt(message, key):
def symmetric_encrypt_HMAC(message, key, hmac_secret): def symmetric_encrypt_HMAC(message, key, hmac_secret):
prefix = random_bytes(3) prefix = random_bytes(3)
hmac = hmac_sha1(hmac_secret, prefix + message)
hmac = HMAC(hmac_secret, SHA1(), backend) iv = hmac[:13] + prefix
hmac.update(prefix)
hmac.update(message)
iv = hmac.finalize()[:13] + prefix
return symmetric_encrypt_with_iv(message, key, iv) return symmetric_encrypt_with_iv(message, key, iv)
def symmetric_encrypt_iv(iv, key): def symmetric_encrypt_iv(iv, key):
@ -81,11 +76,9 @@ def symmetric_decrypt_HMAC(cyphertext, key, hmac_secret):
iv = symmetric_decrypt_iv(cyphertext, key) iv = symmetric_decrypt_iv(cyphertext, key)
message = symmetric_decrypt_with_iv(cyphertext, key, iv) message = symmetric_decrypt_with_iv(cyphertext, key, iv)
hmac = HMAC(hmac_secret, SHA1(), backend) hmac = hmac_sha1(hmac_secret, iv[-3:] + message)
hmac.update(iv[-3:])
hmac.update(message)
if iv[:13] != hmac.finalize()[:13]: if iv[:13] != hmac[:13]:
raise RuntimeError("Unable to decrypt message. HMAC does not match.") raise RuntimeError("Unable to decrypt message. HMAC does not match.")
return message return message
@ -98,6 +91,11 @@ def symmetric_decrypt_with_iv(cyphertext, key, iv):
decryptor = Cipher(AES(key), CBC(iv), backend).decryptor() decryptor = Cipher(AES(key), CBC(iv), backend).decryptor()
return unpad(decryptor.update(cyphertext[BS:]) + decryptor.finalize()) return unpad(decryptor.update(cyphertext[BS:]) + decryptor.finalize())
def hmac_sha1(secret, data):
hmac = HMAC(secret, SHA1(), backend)
hmac.update(data)
return hmac.finalize()
def sha1_hash(data): def sha1_hash(data):
sha = Hash(SHA1(), backend) sha = Hash(SHA1(), backend)
sha.update(data) sha.update(data)

Loading…
Cancel
Save