简体   繁体   中英

AWS signature for signed URL

I came across a .NET example on how to get a signature needed for signing aws request api calls.

I am working on a windows phone 8 app and got stuck with the line

KeyedHashAlgorithm kha = KeyedHashAlgorithm.Create(algorithm);

It appears that windows phone 8 does not have the Create method, the error is below:

'System.Security.Cryptography.KeyedHashAlgorithm' does not contain a definition for Create

Is there an alternative way around this?

Here is the complete code snippet

public static byte[] HmacSHA256(String data, byte[] key)
 {
     String algorithm = "HmacSHA256";
     KeyedHashAlgorithm kha = KeyedHashAlgorithm.Create(algorithm);
     kha.Key = key;

     return kha.ComputeHash(Encoding.UTF8.GetBytes(data));
 }



static byte[] getSignatureKey(String key, String dateStamp, String      regionName, String serviceName)
    {
        byte[] kSecret = Encoding.UTF8.GetBytes(("AWS4" + key).ToCharArray());
        byte[] kDate = HmacSHA256(dateStamp, kSecret);
        byte[] kRegion = HmacSHA256(regionName, kDate);
        byte[] kService = HmacSHA256(serviceName, kRegion);
        byte[] kSigning = HmacSHA256("aws4_request", kService);

        return kSigning;
    }

You don't need to call Create to create an instance of an algorithm, you can just call new to construct the concrete class, eg new HMACSHA256() .

Create is a factory method that allows you to specify the algorithm to use in configuration. In desktops and servers there may be multiple implementations of the same algorithm (native, hardware accelerated etc), or you may want to give users the option to select the algorithm they want.

This is just one of the things that were trimmed from .NET to reduce the runtime size for phones and Silverlight

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM