sdk-world
v2026.11.05
PS2 SDK - world tier (copyleft-allowed, non-essential packages)
Toggle main menu visibility
Loading...
Searching...
No Matches
hmac.h
Go to the documentation of this file.
1
5
// //////////////////////////////////////////////////////////
6
// hmac.h
7
// Copyright (c) 2015 Stephan Brumme. All rights reserved.
8
// see http://create.stephan-brumme.com/disclaimer.html
9
//
10
11
#pragma once
12
13
// based on http://tools.ietf.org/html/rfc2104
14
// see also http://en.wikipedia.org/wiki/Hash-based_message_authentication_code
15
33
34
#include <string>
35
#include <cstring>
// memcpy
36
38
template
<
typename
HashMethod>
39
std::string
hmac
(
const
void
* data,
size_t
numDataBytes,
const
void
* key,
size_t
numKeyBytes)
40
{
41
// initialize key with zeros
42
unsigned
char
usedKey[HashMethod::BlockSize] = {0};
43
44
// adjust length of key: must contain exactly blockSize bytes
45
if
(numKeyBytes <= HashMethod::BlockSize)
46
{
47
// copy key
48
memcpy(usedKey, key, numKeyBytes);
49
}
50
else
51
{
52
// shorten key: usedKey = hashed(key)
53
HashMethod keyHasher;
54
keyHasher.add(key, numKeyBytes);
55
keyHasher.getHash(usedKey);
56
}
57
58
// create initial XOR padding
59
for
(
size_t
i = 0; i < HashMethod::BlockSize; i++)
60
usedKey[i] ^= 0x36;
61
62
// inside = hash((usedKey ^ 0x36) + data)
63
unsigned
char
inside[HashMethod::HashBytes];
64
HashMethod insideHasher;
65
insideHasher.add(usedKey, HashMethod::BlockSize);
66
insideHasher.add(data, numDataBytes);
67
insideHasher.getHash(inside);
68
69
// undo usedKey's previous 0x36 XORing and apply a XOR by 0x5C
70
for
(
size_t
i = 0; i < HashMethod::BlockSize; i++)
71
usedKey[i] ^= 0x5C ^ 0x36;
72
73
// hash((usedKey ^ 0x5C) + hash((usedKey ^ 0x36) + data))
74
HashMethod finalHasher;
75
finalHasher.add(usedKey, HashMethod::BlockSize);
76
finalHasher.add(inside, HashMethod::HashBytes);
77
78
return
finalHasher.getHash();
79
}
80
81
83
template
<
typename
HashMethod>
84
std::string
hmac
(
const
std::string& data,
const
std::string& key)
85
{
86
return
hmac<HashMethod>
(data.c_str(), data.size(), key.c_str(), key.size());
87
}
hmac
std::string hmac(const void *data, size_t numDataBytes, const void *key, size_t numKeyBytes)
compute HMAC hash of data and key using MD5, SHA1 or SHA256
Definition
hmac.h:39
hash
include
hmac.h
Generated by
1.18.0