SHA-256 SAS Signature — Azure Service Bus

SHA-256 SAS Signature — Azure Service Bus

Firma SAS con SHA-256SHA-256 SAS signature

Generación de tokens para Azure Service Bus desde Microsoft Dynamics NAVToken generation for Azure Service Bus from Microsoft Dynamics NAV

C/AL · HMACSHA256
Estructura del token SASSAS token structure

Cualquier cliente que conozca el nombre de una regla de autorización y una de sus claves de firma puede generar un token SAS. El token incluye cuatro valores principales:Any client that knows the name of an authorization rule and one of its signing keys can generate a SAS token. The token contains four main values:

se

Instante de expiración expresado en segundos desde el 1 de enero de 1970 UTC.Expiration time expressed as seconds since January 1, 1970 UTC.

skn

Nombre de la regla de autorización asociada a la clave SAS.Name of the authorization rule associated with the SAS key.

sr

URI completa del recurso de Service Bus al que se solicita acceso.Full URI of the Service Bus resource being accessed.

sig

Firma HMAC-SHA256 codificada para validar el emisor del token.Encoded HMAC-SHA256 signature used to validate the token issuer.

Cómo se calcula la firmaHow the signature is calculated
1Codificar la URIEncode the URI

Aplica URL encoding a la URI completa del recurso.Apply URL encoding to the full resource URI.

2Crear la cadenaBuild the string

Une la URI y la expiración mediante un salto de línea.Join the URI and expiration time with a line feed.

3Firmar y codificarSign and encode

Calcula HMAC-SHA256, convierte a Base64 y aplica URL encoding.Calculate HMAC-SHA256, convert to Base64 and apply URL encoding.

SHA-256('https://<yournamespace>.servicebus.windows.net/' + '\n' + 1438205742)
URI del recursoResource URI

El ámbito forma parte de la firmaThe scope is part of the signature

El destinatario recibe los valores sin el hash, vuelve a calcularlo con los mismos parámetros y verifica que el emisor posee una clave válida. La URI debe identificar por completo el recurso solicitado.The recipient receives the unhashed values, recalculates the hash with the same parameters and verifies that the issuer owns a valid key. The URI must fully identify the requested resource.

https://<namespace>.servicebus.windows.net/
sb://<namespace>.servicebus.windows.net/<entityPath>
https://contoso.servicebus.windows.net/events
SHA-256 signature example
Implementación en C/ALC/AL implementation
Microsoft Dynamics NAV · C/AL
PROCEDURE GetSignatureSHA256@1000000003(
  WebServiceURL@1000000001 : Text;
  pSharedKey@1000000000 : Text;
  VAR pSignature@1000000010 : Text;
  VAR pSignatureExpiration@1000000011 : Text);
VAR
  ExpirySeconds@1000000002 : BigInteger;
  CRLF@1000000003 : Char;
  Crypto@1000000004 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Security.Cryptography.HMACSHA256";
  Encoding@1000000005 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.Encoding";
  Convert@1000000007 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Convert";
  TextToSign@1000000008 : Text;
  HttpUtility@1000000009 : DotNet "'System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.System.Web.HttpUtility";
BEGIN
  CLEAR(pSignature);
  CLEAR(pSignatureExpiration);
  CRLF := 10;

  ExpirySeconds := ROUND((CURRENTDATETIME - CREATEDATETIME(010170D,000000T)) / 1000,1) + 7200;
  TextToSign := HttpUtility.UrlEncode(WebServiceURL) + FORMAT(CRLF) + FORMAT(ExpirySeconds);

  Crypto := Crypto.HMACSHA256(Encoding.ASCII.GetBytes(pSharedKey));

  pSignatureExpiration := FORMAT(ExpirySeconds);
  pSignature := HttpUtility.UrlEncode(
    Convert.ToBase64String(
      Crypto.ComputeHash(Encoding.ASCII.GetBytes(TextToSign))));
END;
Nota:Note: este ejemplo utiliza interoperabilidad .NET de versiones clásicas de Microsoft Dynamics NAV y establece una validez de 7200 segundos. this example uses .NET interoperability from classic Microsoft Dynamics NAV versions and sets a validity period of 7200 seconds.
Azure Service Bus · Shared Access Signature · HMAC-SHA256

Comentarios