How to calculate PublisherID from Publisher?
I was very close in my guess of how the PublisherId value is constructed. I heard some whispering...
The value is a Crockfordâs Base32 encoding of the first eight bytes of SHA-256 hash of the publisher string in UTF-16 (little endian). I have a good implementation that I validated using the sample values from my question.
Or you simply use the Kernel32.dll Method PackageFamilyNameFromId:
private static string GetPackageFamilyName(string name, string publisherId)
{
string packageFamilyName = null;
PACKAGE_ID packageId = new PACKAGE_ID
{
name = name,
publisher = publisherId
};
uint packageFamilyNameLength = 0;
//First get the length of the Package Name -> Pass NULL as Output Buffer
if (PackageFamilyNameFromId(packageId, ref packageFamilyNameLength, null) == 122) //ERROR_INSUFFICIENT_BUFFER
{
StringBuilder packageFamilyNameBuilder = new StringBuilder((int)packageFamilyNameLength);
if (PackageFamilyNameFromId(packageId, ref packageFamilyNameLength, packageFamilyNameBuilder) == 0)
{
packageFamilyName = packageFamilyNameBuilder.ToString();
}
}
return packageFamilyName;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 4)]
private class PACKAGE_ID
{
public uint reserved;
public uint processorArchitecture;
public ulong version;
public string name;
public string publisher;
public string resourceId;
public string publisherId;
}
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
private static extern uint PackageFamilyNameFromId(PACKAGE_ID packageId, ref uint packageFamilyNameLength, StringBuilder packageFamilyName);
public static void TestMethod()
{
GetPackageFamilyName("MyCompany.TestApp", "CN=YourPublisherId");
}