New function xs_sha256_base64().

This commit is contained in:
default 2022-09-21 09:17:59 +02:00
parent 8dadbbc78e
commit 9573dbcdb6
2 changed files with 24 additions and 4 deletions

4
http.c
View File

@ -43,9 +43,9 @@ d_char *http_signed_request(snac *snac, char *method, char *url,
/* digest */ /* digest */
if (body != NULL) if (body != NULL)
digest = xs_sha256_hex(body, b_size); digest = xs_sha256_base64(body, b_size);
else else
digest = xs_sha256_hex("", 0); digest = xs_sha256_base64("", 0);
seckey = xs_dict_get(snac->key, "secret"); seckey = xs_dict_get(snac->key, "secret");

View File

@ -7,6 +7,7 @@
d_char *xs_md5_hex(const void *input, int size); d_char *xs_md5_hex(const void *input, int size);
d_char *xs_sha1_hex(const void *input, int size); d_char *xs_sha1_hex(const void *input, int size);
d_char *xs_sha256_hex(const void *input, int size); d_char *xs_sha256_hex(const void *input, int size);
d_char *xs_sha256_base64(const void *input, int size);
d_char *xs_rsa_genkey(int bits); d_char *xs_rsa_genkey(int bits);
d_char *xs_rsa_sign(char *secret, char *mem, int size); d_char *xs_rsa_sign(char *secret, char *mem, int size);
int xs_rsa_verify(char *pubkey, char *mem, int size, char *b64sig); int xs_rsa_verify(char *pubkey, char *mem, int size, char *b64sig);
@ -45,19 +46,38 @@ d_char *xs_sha1_hex(const void *input, int size)
} }
d_char *xs_sha256_hex(const void *input, int size) unsigned char *_xs_sha256(const void *input, int size, unsigned char *sha256)
{ {
unsigned char sha256[32];
SHA256_CTX ctx; SHA256_CTX ctx;
SHA256_Init(&ctx); SHA256_Init(&ctx);
SHA256_Update(&ctx, input, size); SHA256_Update(&ctx, input, size);
SHA256_Final(sha256, &ctx); SHA256_Final(sha256, &ctx);
return sha256;
}
d_char *xs_sha256_hex(const void *input, int size)
{
unsigned char sha256[32];
_xs_sha256(input, size, sha256);
return xs_hex_enc((char *)sha256, sizeof(sha256)); return xs_hex_enc((char *)sha256, sizeof(sha256));
} }
d_char *xs_sha256_base64(const void *input, int size)
{
unsigned char sha256[32];
_xs_sha256(input, size, sha256);
return xs_base64_enc((char *)sha256, sizeof(sha256));
}
d_char *xs_rsa_genkey(int bits) d_char *xs_rsa_genkey(int bits)
/* generates an RSA keypair */ /* generates an RSA keypair */
{ {