summaryrefslogtreecommitdiffstats
path: root/source/core/slang-digest-util.cpp
blob: d235f2cf075af3c2c386c07c4fd084d1fe3303e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// string-digest-util.cpp
#include "slang-digest-util.h"

#include "../core/slang-basic.h"
#include "../core/slang-digest-builder.h"
#include "../core/slang-md5.h"
#include "../core/slang-char-util.h"

namespace Slang
{

/*static*/ Digest DigestUtil::computeDigestForStringSlice(UnownedStringSlice text)
{
    DigestBuilder builder;
    builder.addToDigest(text);
    return builder.finalize();
}

/*static*/ Digest DigestUtil::combine(const Digest& digestA, const Digest& digestB)
{
    DigestBuilder builder;
    builder.addToDigest(digestA);
    builder.addToDigest(digestB);
    return builder.finalize();
}

/*static*/ String DigestUtil::toString(const Digest& digest)
{
    StringBuilder hashString;

    uint8_t* uint8Hash = (uint8_t*)digest.values;

    for (Index i = 0; i < 16; ++i)
    {
        auto hashSegmentString = String(uint8Hash[i], 16);

        if (hashSegmentString.getLength() == 1)
        {
            hashString.append("0");
        }
        hashString.append(hashSegmentString.getBuffer());
    }

    return hashString;
}

/*static*/ Digest DigestUtil::fromString(UnownedStringSlice hashString)
{
    uint8_t uint8Hash[16];

    // When the hash is converted to a String, ReverseInternalAscii is called
    // at the very end. Since there is no way to get a char* for hashString to pass
    // to ReverseInternalAscii to flip the string back, we instead loop starting from
    // the end and work backwards towards the beginning.
    for (Index i = 0; i < 16; i++)
    {
        uint8Hash[i] = (uint8_t)CharUtil::getHexDigitValue(hashString[i * 2]) * 16
            + (uint8_t)CharUtil::getHexDigitValue(hashString[i * 2 + 1]);
    }

    Digest digest;
    memcpy(digest.values, uint8Hash, 16);
    return digest;
}

}