Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added HmacSha256Signature class #197

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ virtualenv/*
.coverage
!.gitignore
!.gitmodules
venv/*
/.project
/.pydevproject
/.settings
26 changes: 18 additions & 8 deletions rauth/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import hmac

from hashlib import sha1
from hashlib import sha256

from rauth.compat import is_basestring, quote, urlencode, urlsplit, urlunsplit
from rauth.utils import FORM_URLENCODED
Expand Down Expand Up @@ -102,16 +103,17 @@ def _normalize_request_parameters(self, oauth_params, req_kwargs):
.replace('+', '%20')\
.replace('%7E', '~')


class HmacSha1Signature(SignatureMethod):
class HmacShaAnySignature(SignatureMethod):
'''
HMAC-SHA1 Signature Method.
Parent class for HMAC-SHA1 or HMAC-SHA256 Signature Method.

This is a signature method, as per the OAuth 1.0/a specs. As the name
might suggest, this method signs parameters with HMAC using SHA1.
might suggest, this method signs parameters with HMAC using SHA1 or SHA256.
'''
NAME = 'HMAC-SHA1'

def __init__(self, name, sha_hash):
self.NAME = name
self.HASH = sha_hash

def sign(self,
consumer_secret,
access_token_secret,
Expand Down Expand Up @@ -148,12 +150,20 @@ def sign(self,
# build a Signature Base String
signature_base_string = b'&'.join(parameters)

# hash the string with HMAC-SHA1
hashed = hmac.new(key, signature_base_string, sha1)
# hash the string with HMAC-SHA*
hashed = hmac.new(key, signature_base_string, self.HASH)

# return the signature
return base64.b64encode(hashed.digest()).decode()

class HmacSha1Signature(HmacShaAnySignature):
def __init__(self):
HmacShaAnySignature.__init__(self, 'HMAC-SHA1', sha1)

class HmacSha256Signature(HmacShaAnySignature):
def __init__(self):
HmacShaAnySignature.__init__(self, 'HMAC-SHA256', sha256)


class RsaSha1Signature(SignatureMethod):
'''
Expand Down