Reimplement the uniqueString() hash ARM function

551 Views Asked by At

The uniqueString function in ARM is a hash function that returns 13 chars. Executing the the function in the small script shown below richard for example becomes b6oys6fwmheio.

Does anyone know how this is implemented so one can reimplement it in code?

I'd be really handy to have when one creates and name things in the portal, and then just update it when deploying through ARM later.

ARM script for executing the function

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [],
  "outputs": {
    "uniqueName": {
      "value": "[uniqueString('richard')]",
      "type": "string"
    }
  }
}

Just using the standard hash algorithms and a very naive implementation doesn't really get you anywhere close either, as shown in this fiddle. So it'd be really nice to get some details of the implementation.

  • Sha1 gave lujs8awci6eim
  • Sha256 gave junhrxwwbjrth
  • Sha512 gave slqvqdqjz3xcx
1

There are 1 best solutions below

1
On

A non-elegant solution (im not a PS dev :) ):

param($text)

$tempPath = (New-TemporaryFile).FullName;
@'
{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "text": {
            "type": "string"
        }
    },
    "resources": [],
    "outputs": {
        "uniqueName": {
            "value": "[uniqueString(parameters('text'))]",
            "type": "string"
        }
    }
}
'@ > $tempPath;

$result = New-AzDeployment -Location "westeurope" -TemplateFile $tempPath -TemplateParameterObject @{ text = $text };
return $result.Outputs.uniqueName.value;