Azure ARM uniqueString mimic function
I need to deploy Sql databases to Azure Sql server using ways: ARM template path and more convenient way to use C # code. There is an ARM template function called uniqueString(string)
that generates a pseudo-random hash of a given string. This is a deterministic pure function.
I need to find a way to accurately reproduce the behavior of this function from my C # code. those. I need to reproduce this function in my C # code.
Where can I find the algorithm used by ARM Api?
+3
source to share
2 answers
I found PowerShell code to do it here: https://blogs.technet.microsoft.com/389thoughts/2017/12/23/get-uniquestring-generate-unique-id-for-azure-deployments/
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;
}
+1
source to share