Calculating hash values with NAV2013

Here is a simple way to calculate SHA256 hashes with Microsoft Dynamics NAV 2013.

(There is a easier way if you’re using NAV2018 – Read here)

Download the Microsoft Dynamics NAV 2013 object here: SHA256

Share thisShare on FacebookTweet about this on TwitterShare on RedditShare on Google+Share on LinkedIn

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
    key = ‘wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY’
    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
    CLEAR(SHA256Lib);
    SHA256Lib.InitSHA256;
    SHA256Lib.AddString(AsciiAnsiLib.Ansi2Ascii(‘AWS4’ + key));
    SHA256Lib.AddLF;
    SHA256Lib.AddString(DateStamp);
    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