重新实现uniqueString哈希ARM函数

问题描述

ARM中的uniqueString函数一个哈希函数,返回13个字符。例如,在 <category android:name="android.intent.category.DEFAULT" /> 下面显示的小脚本中执行该功能就变成了richard

有人知道它是如何实现的,以便可以在代码中重新实现吗?

当人们在门户中创建并命名事物,然后稍后通过ARM进行部署时,只要对其进行更新,我将非常方便。

用于执行功能的ARM脚本

b6oys6fwmheio

仅使用标准的哈希算法和非常幼稚的实现并不能真正使您接近任何地方,如本fiddle所示。因此,非常了解实现的一些细节。

  • Sha1给了{ "$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" } } }
  • Sha256给了lujs8awci6eim
  • Sha512给了junhrxwwbjrth

解决方法

非优雅的解决方案(不是PS开发人员:)):

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;