Azure ARM uniqueString 函数模拟 [英] Azure ARM uniqueString function mimic
问题描述
我需要使用以下方式将 Sql 数据库部署到 Azure Sql Server:ARM 模板方式,以及使用 C# 代码的更自定义方式.有一个名为 uniqueString(string)
的 ARM 模板函数,它生成给定字符串的伪随机散列.这是一个确定性纯函数.
I need to deploy Sql Databases into an Azure Sql Server using to ways: the ARM template way, and a more custom way using C# code. There's a ARM template function called uniqueString(string)
that generate a pseudo random hash of a given string. It's a deterministic pure function.
我需要找到一种方法来从我的 C# 代码中准确模拟此函数的行为.即我需要将此函数重现到我的 C# 代码中.
I need to find a way to exactly mimic the behaviour of this function from my C# code. ie I need to reproduce this function into my C# code.
在哪里可以找到 ARM Api 使用的算法?
Where can i find the algorithm used by the ARM Api ?
推荐答案
我在这里找到了一些 PowerShell 代码:https://blogs.technet.microsoft.com/389thoughts/2017/12/23/get-uniquestring-generate-unique-id-for-azure-deployments/
I found some PowerShell code to do this here: https://blogs.technet.microsoft.com/389thoughts/2017/12/23/get-uniquestring-generate-unique-id-for-azure-deployments/
我将此代码转换为 C#:
I converted this code to C#:
public string GetUniqueString(string id, int length = 13)
{
string result = "";
var buffer = System.Text.Encoding.UTF8.GetBytes(id);
var hashArray = new System.Security.Cryptography.SHA512Managed().ComputeHash(buffer);
for(int i = 1; i <= length; i++)
{
var b = hashArray[i];
var c = Convert.ToChar((b % 26) + (byte)'a');
result = result + c;
}
return result;
}
这篇关于Azure ARM uniqueString 函数模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!