3 thoughts on “Calculating hash values with NAV2013”

  1. Hi, thanks for the SHA256 code. I think I may be misusing it, I’m trying to create a signature for Amazon Web Services, (http://docs.aws.amazon.com/general/latest/gr/signature-v4-examples.html) as below (first step). Any help would be greatly appreciated. Thanks Roddy

    Test Input
    dateStamp = ‘20120215’

    Expected Output
    kDate = ‘969fbb94feb542b71ede6f87fe4d5fa29c789342b0f407474670f0c2489e0a0d’

    Actual Output
    kDate = ‘8583c661110d4c95a37df70fd6f70d92fd18418f306c9f8d54582a2c354acad4”

    Sample Psuedo code from AWS
    kDate = HMAC(“AWS4” + key, dateStamp) //UTF-8

    My use of your library
    SHA256Lib.AddString(AsciiAnsiLib.Ansi2Ascii(‘AWS4’ + key));
    kDate := SHA256Lib.CalculateSHA256;

    1. You cannot use SHA256 for HMACSHA256 – see http://security.stackexchange.com/questions/79577/whats-the-difference-between-hmac-sha256key-data-and-sha256key-data

      You much recreate this C# example from Amazon in C/AL

      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;

Leave a Reply