在Vala中编写Hmac函数

时间:2012-03-13 19:58:21

标签: hmac vala hmacsha1

我正在Vala for Windows和Linux上编写跨平台应用程序。我需要实现Hmac的安全性;但不幸的是,GHmac类(link)尚未移植到Windows。我在维基百科(link)上找到了Hmac的算法,我相信我已经正确实现了它,但是当与内置类比较时,我得不到相同的结果。以下是我的功能,如果有人能帮我找到令人惊奇的错误。

public static string compute_for_data(ChecksumType type, uint8[] key,
                                                            uint8[] data) {
    int block_size = 64;
    uint8[] mod_key = key;
    uint8[] outer = new uint8[block_size];
    uint8[] inner = new uint8[block_size];

    if (mod_key.length > block_size) {
        mod_key = Checksum.compute_for_data(type, key).data;
    }
    mod_key.resize(block_size);

    for (int i=0; i < mod_key.length; i++) {
        outer[i] = mod_key[i] ^ 0x5c;
        inner[i] = mod_key[i] ^ 0x36;
    }

    int i = inner.length;
    inner.resize(i + data.length);
    for (int j=0; j < data.length; j++) {
        inner[i + j] = data[j];
    }

    inner = Checksum.compute_for_data(type, inner).data;

    i = outer.length;
    outer.resize(i + inner.length);
    for (int j=0; j < inner.length; j++) {
        outer[i + j] = inner[j];
    }

    return Checksum.compute_for_data(type, outer);
}

1 个答案:

答案 0 :(得分:2)

我知道回答一个人自己的问题很俗气,但我设法在朋友的帮助下解决这个问题,所以这里是解决方案。基本上当我使用Checksum.compute_for_data函数时,它返回一个十六进制字符串而不是十六进制数据,这打破了算法。这是更正后的版本:

public static string compute_for_data(ChecksumType type, uint8[] key,
                                                            uint8[] data) {
    int block_size = 64;
    switch (type) {
        case ChecksumType.MD5:
        case ChecksumType.SHA1:
            block_size = 64; /* RFC 2104 */
            break;
        case ChecksumType.SHA256:
            block_size = 64; /* RFC draft-kelly-ipsec-ciph-sha2-01 */
            break;
    }

    uint8[] buffer = key;
    if (key.length > block_size) {
        buffer = Checksum.compute_for_data(type, key).data;
    }
    buffer.resize(block_size);

    Checksum inner = new Checksum(type);
    Checksum outer = new Checksum(type);

    uint8[] padding = new uint8[block_size];
    for (int i=0; i < block_size; i++) {
        padding[i] = 0x36 ^ buffer[i];
    }
    inner.update(padding, padding.length);
    for (int i=0; i < block_size; i++) {
        padding[i] = 0x5c ^ buffer[i];
    }
    outer.update(padding, padding.length);

    size_t length = buffer.length;
    inner.update(data, data.length);
    inner.get_digest(buffer, ref length);

    outer.update(buffer, length);
    return outer.get_string();
}