-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.c
More file actions
38 lines (35 loc) · 1.32 KB
/
Copy pathutils.c
File metadata and controls
38 lines (35 loc) · 1.32 KB
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
#include "utils.h"
void generatePseudoRandomString(char *generatedStr) {
uint8_t buf[SESSION_ID_STRLEN];
randombytes_buf(buf, SESSION_ID_STRLEN);
for (uint16_t i = 0; i < SESSION_ID_STRLEN; i++)
generatedStr[i] =
SESSION_ID_DICTIONARY[buf[i] % SESSION_ID_DICTIONARY_STRLEN];
}
void signData(const unsigned char *key, const size_t keyLen,
const unsigned char *data, const size_t dataLen,
char *signature) {
unsigned char signatureHash[crypto_auth_hmacsha256_BYTES];
crypto_auth_hmacsha256_state state;
crypto_auth_hmacsha256_init(&state, key, keyLen);
crypto_auth_hmacsha256_update(&state, data, dataLen);
crypto_auth_hmacsha256_final(&state, signatureHash);
sodium_bin2hex(signature, SIGNATURE_STRLEN + 1, signatureHash,
32); // 32 bytes = 256 bits.
}
void parseToken(const char *token, char *tokenVersion, char *sessionId,
char *signature) {
uint8_t i = 0;
uint8_t ti = 0;
for (i = 0; i < TOKEN_VERSION_STRLEN; i++, ti++)
tokenVersion[i] = token[ti];
tokenVersion[TOKEN_VERSION_STRLEN] = '\0';
ti++;
for (i = 0; i < SESSION_ID_STRLEN; i++, ti++)
sessionId[i] = token[ti];
sessionId[SESSION_ID_STRLEN] = '\0';
ti++;
for (i = 0; i < SIGNATURE_STRLEN; i++, ti++)
signature[i] = token[ti];
signature[SIGNATURE_STRLEN] = '\0';
}