如何在user.config路径中获取哈希值? [英] How to get hash value in user.config path?

查看:52
本文介绍了如何在user.config路径中获取哈希值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了.NET应用程序.它的配置位置是

I have installed .NET application. Its config location is

%AppData%\ [CompanyName] \ [ExeName] _Url_ [hash] \ [version] \ user.config.

我需要从另一个应用程序获取 [hash] 值.

I need to get [hash] value from another application.

根据 MSDN user.config 路径模板为

[c:\ Documents and Settings] \ [username] \ [Local Settings] \ Application Data \ [companyname] \ [appdomainname] _ [eid] _ [hash] \ [version]

其中 [hash] 是SHA1证据哈希(在我的情况下为eid = Url).

where [hash] is SHA1 hash of evidence (in my case eid=Url).

我注意到以下情况:

  • [hash] 随应用程序安装路径的变化而变化.
  • [hash] 始终为32个字符长,因此它不是SHA1的十六进制表示形式,SHA1的长度为40个字符.似乎 [hash] = base32(sha1([安装路径]))
  • [hash] changes with application installation path changes.
  • [hash] is always 32 characters long, so it is not hex representation of SHA1 which is 40 characters long. It seems that [hash]=base32(sha1([install path]))

我为 [安装路径]

c:\ Program Files ...
file:///c:\ Program Files ....
file:///c:\ Program%20Files ...等

c:\Program Files...
file:///c:\Program Files....
file:///c:\Program%20Files..., etc

但是 [hash] 总是错误的.

推荐答案

在经历了尝试计算程序的 user.config (本地)路径的相同问题之后,我决定对@Gerard进行补充Sexton的答案以及代码段.假设 [eid] 等于"Url" ,并且 [appdomainname] 是可执行文件名,则采用以下方法.>

After going through the same problem of trying to calculate a program's user.config (local) path, i decided to complement @Gerard Sexton 's answer with a code snippet. The following method was done with the assumptions that the [eid] equals "Url" and [appdomainname] is the executable file name.

public string GetExeLocalAppDataUserConfigPath(string fullExePath)
{
    //E.g.: fullExePath = @"C:\Program Files (x86)\MyExeFolder\MyProgram.exe"
    var localAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

    var versionInfo = FileVersionInfo.GetVersionInfo(fullExePath);
    var companyName = versionInfo.CompanyName;
    var exeName = versionInfo.OriginalFilename;// or 'AppDomain.CurrentDomain.FriendlyName'

    var assemblyName = AssemblyName.GetAssemblyName(fullExePath);
    var version = assemblyName.Version.ToString();

    var uri = "file:///" + fullExePath; //or 'assemblyName.CodeBase' if vshost (you can check the 'FriendlyName')
    uri = uri.ToUpperInvariant();

    var ms = new MemoryStream();
    var bSer = new BinaryFormatter();
    bSer.Serialize(ms, uri);
    ms.Position = 0;
    var sha1 = new SHA1CryptoServiceProvider();
    var hash = sha1.ComputeHash(ms);
    var hashstring = ToBase32StringSuitableForDirName(hash);

    //<AppData Local User Path> + <Company Name> + <[ExeName]_[eid]_[Hash]> + <Version> + user.config
    var userConfigLocalAppDataPath = Path.Combine(localAppDataPath, companyName, exeName+"_Url_"+hashstring, version, "user.config");

    return userConfigLocalAppDataPath;
}

这是在Gerard的

And here is the ToBase32StringSuitableForDirName implementation found in Gerard's link!

static Char[] s_Base32Char = {
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 
            'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
            'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 
            'y', 'z', '0', '1', '2', '3', '4', '5'};

private static string ToBase32StringSuitableForDirName(byte[] buff)
{
    StringBuilder sb = new StringBuilder();
    byte b0, b1, b2, b3, b4;
    int l, i;

    l = buff.Length;
    i = 0;

    // Create l chars using the last 5 bits of each byte.  
    // Consume 3 MSB bits 5 bytes at a time.

    do
    {
        b0 = (i < l) ? buff[i++] : (byte)0;
        b1 = (i < l) ? buff[i++] : (byte)0;
        b2 = (i < l) ? buff[i++] : (byte)0;
        b3 = (i < l) ? buff[i++] : (byte)0;
        b4 = (i < l) ? buff[i++] : (byte)0;

        // Consume the 5 Least significant bits of each byte
        sb.Append(s_Base32Char[b0 & 0x1F]);
        sb.Append(s_Base32Char[b1 & 0x1F]);
        sb.Append(s_Base32Char[b2 & 0x1F]);
        sb.Append(s_Base32Char[b3 & 0x1F]);
        sb.Append(s_Base32Char[b4 & 0x1F]);

        // Consume 3 MSB of b0, b1, MSB bits 6, 7 of b3, b4
        sb.Append(s_Base32Char[(
            ((b0 & 0xE0) >> 5) |
            ((b3 & 0x60) >> 2))]);

        sb.Append(s_Base32Char[(
            ((b1 & 0xE0) >> 5) |
            ((b4 & 0x60) >> 2))]);

        // Consume 3 MSB bits of b2, 1 MSB bit of b3, b4

        b2 >>= 5;

        if ((b3 & 0x80) != 0)
            b2 |= 0x08;
        if ((b4 & 0x80) != 0)
            b2 |= 0x10;

        sb.Append(s_Base32Char[b2]);

    } while (i < l);

    return sb.ToString();
}

这篇关于如何在user.config路径中获取哈希值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆