Crypto.CreateHash

Updated on

Crypto.CreateHash is a Power Query M function that computes a cryptographic hash for a given binary value using a specified cryptographic algorithm.

Compatible with: Power BI Service Power BI Desktop Excel Microsoft 365

Syntax

Crypto.CreateHash(
   algorithm as number,
   value as binary,
) as binary

Description

The Crypto.CreateHash function generates a cryptographic hash from the input data using a specified hashing algorithm. Its two arguments are the algorithm (e.g., CryptoAlgorithm.SHA256) and the input data (binary data, often converted from text).

Examples

Here’s an example that uses Crypto.CreateHash for a codeChallenge:

let
  // Helper variables
  client_id = "<AUTH0 CLIENT ID>",
  redirect_uri = "https://oauth.powerbi.com/views/oauthredirect.html",
  authorize_uri = "https://<YOUR_AUTH_DOMAIN>.com/authorize",
  token_uri = "https://<YOUR_AUTH_DOMAIN>.com/oauth/token",
  windowWidth = 1200,
  windowHeight = 1000,
  codeVerifier = Text.NewGuid() & Text.NewGuid(),

// Start Login function
StartLogin = (resourceUrl, state, display) =>

  params = Json.Document(resourceUrl),
  codeChallenge = Base64UrlEncodeWithoutPadding( Crypto.CreateHash( CryptoAlgorithm.SHA256, Text.ToBinary( codeVerifier, TextEncoding.Ascii))),
  AuthorizeUrl = authorize_uri & "?" 
    & Uri.BuildQueryString( 
       [
        client_id = client_id,
        scope = "openid offline_access",
        response_type = "code",
        state = state,
        redirect_uri = redirect_uri,
        code_challenge_method = "S256",
        code_challenge = codeChallenge
      ]
    )
  
in
  [
      LoginUri = AuthorizeUrl,
      CallbackUri = redirect_uri,
      WindowHeight = windowHeight,
      WindowWidth = windowWidth,
      Context = []
  ]

The above code has been taken from the following forum.

Another forum example uses the function in below code:

Base64UrlEncodeWithoutPadding = (hash as binary) as text =>
    let
        base64Encoded = Binary.ToText(hash, BinaryEncoding.Base64),
        base64UrlEncoded = Text.Replace(Text.Replace(base64Encoded, "+", "-"), "/", "_"),
        withoutPadding = Text.TrimEnd(base64UrlEncoded, "=")
    in 
        withoutPadding;

MyConnector.StartLogin = (resourceUrl, state, display) =>
    let
        baseUri = ..., //Generate uri from resourceUrl
        codeVerifier = Text.NewGuid() & Text.NewGuid(),
        codeChallenge = Base64UrlEncodeWithoutPadding(Crypto.CreateHash(CryptoAlgorithm.SHA256, Text.ToBinary(codeVerifier, TextEncoding.Ascii))),
        authorizeUrl = baseUri & "/oauth2/authorize?" & Uri.BuildQueryString([
            client_id = clientId,
            scope = tokenScope,
            state = state,
            code_challenge_method = "S256",
            code_challenge = codeChallenge,
            redirect_uri = redirectUri,
            response_type = "code"])
    in
        [
            LoginUri = authorizeUrl,
            CallbackUri = redirectUri,
            WindowHeight = 720,
            WindowWidth = 1024,
            Context = [BaseUri = baseUri, CodeVerifier = codeVerifier]
        ];

Other functions related to Crypto.CreateHash are:

Contribute » | Contributors: Rick de Groot

2023-2024 © BI Gorilla. All rights are reserved. Information from Microsoft docs is property of Microsoft Corp. | Privacy Policy