1

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
Riri
  • 11,501
  • 14
  • 63
  • 88
  • 1
    I dont have bw to check, does this work: https://stackoverflow.com/questions/43295720/azure-arm-uniquestring-function-mimic – 4c74356b41 Sep 29 '20 at 12:42
  • No ... I doesn't unfortunately ... It's just a way to implement something similar - but not an identical hash. – Riri Sep 30 '20 at 06:24
  • fffffff, i swear to god, there was an answer from @bmoore-msft with the exact hashing alg they are using, but I cant find it >.. – 4c74356b41 Sep 30 '20 at 07:09
  • @bmoore-msft – Riri Sep 30 '20 at 07:23
  • Just pulling some threads together - I posted an answer on a duplicate question - see https://stackoverflow.com/a/67399362/3156906. It's not a full standalone implementation (it requires some reference binaries) but it *does* give the correct results... – mclayton May 05 '21 at 10:34

1 Answers1

0

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;
Jonas Jämtberg
  • 573
  • 2
  • 6
  • Thanks. I was however hoping to be able to implement it in code and run locally, without having to create an Azure deployment and run it there. – Riri Sep 30 '20 at 12:57