from System import Convert
from System.Security.Cryptography import HMACSHA256
from System import String
from System.Text.Encoding import GetBytes
from System.Text import Encoding
from System.Globalization import CultureInfo

from orangescape.model.server.azure.storageconfig import SharedKeyAuthorizationScheme, Account

def CreateAuthorizationHeader(canonicalizedString, Key):
    signature = CreateHmacSignature(canonicalizedString, Key)
    authorizationHeader = String.Format(CultureInfo.InvariantCulture, "{0} {1}:{2}", SharedKeyAuthorizationScheme, Account, signature);
    return authorizationHeader

def CreateHmacSignature(unsignedString, key):
    enc = Encoding.UTF8
    dataToHmac = enc.GetBytes(unsignedString)
    hmacSha256 = HMACSHA256(key)
    return Convert.ToBase64String(hmacSha256.ComputeHash(dataToHmac))
