mirror of
https://git.telodendria.io/Telodendria/Cytoplasm.git
synced 2024-11-22 12:10:47 +03:00
Refactor Sha API to return raw bytes, added Sha1() function.
This commit is contained in:
parent
280838ddd6
commit
f5abcf5e50
3 changed files with 104 additions and 35 deletions
|
@ -21,30 +21,28 @@
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
* SOFTWARE.
|
* SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
#include <Sha.h>
|
||||||
|
#include <Memory.h>
|
||||||
|
|
||||||
#ifndef CYTOPLASM_SHA2_H
|
#include <stdio.h>
|
||||||
#define CYTOPLASM_SHA2_H
|
#include <string.h>
|
||||||
|
|
||||||
/***
|
char *
|
||||||
* @Nm Sha2
|
ShaToHex(unsigned char *bytes)
|
||||||
* @Nd A simple implementation of the SHA2 hashing functions.
|
{
|
||||||
* @Dd December 19 2022
|
size_t i = 0;
|
||||||
* @Xr Memory Base64
|
char *str = Malloc(((strlen((char *) bytes) * 2) + 1) * sizeof(char));
|
||||||
*
|
|
||||||
* This API defines simple functions for computing SHA2 hashes.
|
|
||||||
* At the moment, it only defines
|
|
||||||
* .Fn Sha256 ,
|
|
||||||
* which computes the SHA-256 hash of the given C string. It is
|
|
||||||
* not trivial to implement SHA-512 in ANSI C due to the lack of
|
|
||||||
* a 64-bit integer type, so that hash function has been omitted.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
if (!str)
|
||||||
* This function takes a pointer to a NULL-terminated C string, and
|
{
|
||||||
* returns a string allocated on the heap using the Memory API, or
|
return NULL;
|
||||||
* NULL if there was an error allocating memory. The returned string
|
}
|
||||||
* should be freed when it is no longer needed.
|
|
||||||
*/
|
|
||||||
extern char * Sha256(char *);
|
|
||||||
|
|
||||||
#endif /* CYTOPLASM_SHA2_H */
|
while (bytes[i] != '\0')
|
||||||
|
{
|
||||||
|
snprintf(str + (2 * i), 3, "%02x", bytes[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return str;
|
||||||
|
}
|
|
@ -21,7 +21,7 @@
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
* SOFTWARE.
|
* SOFTWARE.
|
||||||
*/
|
*/
|
||||||
#include <Sha2.h>
|
#include <Sha.h>
|
||||||
#include <Memory.h>
|
#include <Memory.h>
|
||||||
#include <Int.h>
|
#include <Int.h>
|
||||||
|
|
||||||
|
@ -170,13 +170,12 @@ Sha256Process(Sha256Context * context, unsigned char *data, size_t length)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
char *
|
unsigned char *
|
||||||
Sha256(char *str)
|
Sha256(char *str)
|
||||||
{
|
{
|
||||||
Sha256Context context;
|
Sha256Context context;
|
||||||
size_t i;
|
size_t i;
|
||||||
unsigned char out[32];
|
unsigned char *out;
|
||||||
char *outStr;
|
|
||||||
|
|
||||||
unsigned char fill[64];
|
unsigned char fill[64];
|
||||||
UInt32 fillLen;
|
UInt32 fillLen;
|
||||||
|
@ -189,8 +188,8 @@ Sha256(char *str)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
outStr = Malloc(65);
|
out = Malloc(33 * sizeof(unsigned char));
|
||||||
if (!outStr)
|
if (!out)
|
||||||
{
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -228,11 +227,7 @@ Sha256(char *str)
|
||||||
PUT_UINT32(&out[4 * i], context.state[i]);
|
PUT_UINT32(&out[4 * i], context.state[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Convert to string */
|
out[32] = '\0';
|
||||||
for (i = 0; i < 32; i++)
|
|
||||||
{
|
|
||||||
snprintf(outStr + (2 * i), 3, "%02x", out[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return outStr;
|
return out;
|
||||||
}
|
}
|
76
src/include/Sha.h
Normal file
76
src/include/Sha.h
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person
|
||||||
|
* obtaining a copy of this software and associated documentation files
|
||||||
|
* (the "Software"), to deal in the Software without restriction,
|
||||||
|
* including without limitation the rights to use, copy, modify, merge,
|
||||||
|
* publish, distribute, sublicense, and/or sell copies of the Software,
|
||||||
|
* and to permit persons to whom the Software is furnished to do so,
|
||||||
|
* subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be
|
||||||
|
* included in all copies or portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||||
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef CYTOPLASM_SHA_H
|
||||||
|
#define CYTOPLASM_SHA_H
|
||||||
|
|
||||||
|
/***
|
||||||
|
* @Nm Sha
|
||||||
|
* @Nd A simple implementation of a few SHA hashing functions.
|
||||||
|
* @Dd December 19 2022
|
||||||
|
* @Xr Memory Base64
|
||||||
|
*
|
||||||
|
* This API defines simple functions for computing SHA hashes.
|
||||||
|
* At the moment, it only defines
|
||||||
|
* .Fn Sha256
|
||||||
|
* and
|
||||||
|
* .Fn Sha1 ,
|
||||||
|
* which compute the SHA-256 and SHA-1 hashes of the given C string,
|
||||||
|
* respectively. It is not trivial to implement SHA-512 in ANSI C
|
||||||
|
* due to the lack of a 64-bit integer type, so that hash
|
||||||
|
* function has been omitted.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This function takes a pointer to a NULL-terminated C string, and
|
||||||
|
* returns a NULL-terminated byte buffer allocated on the heap using
|
||||||
|
* the Memory API, or NULL if there was an error allocating memory.
|
||||||
|
* The returned byte buffer should be freed when it is no longer
|
||||||
|
* needed. It is important to note that the returned buffer is not
|
||||||
|
* a printable string; to get a printable string, use
|
||||||
|
* .Fn ShaToHex .
|
||||||
|
*/
|
||||||
|
extern unsigned char * Sha256(char *);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This function takes a pointer to a NULL-terminated C string, and
|
||||||
|
* returns a NULL-terminated byte buffer allocated on the heap using
|
||||||
|
* the Memory API, or NULL if there was an error allocating memory.
|
||||||
|
* The returned byte buffer should be freed when it is no longer
|
||||||
|
* needed. It is important to note that the returned buffer is not
|
||||||
|
* a printable string; to get a printable string, use
|
||||||
|
* .Fn ShaToHex .
|
||||||
|
*/
|
||||||
|
extern unsigned char * Sha1(char *);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a SHA byte buffer into a hex string. These hex strings
|
||||||
|
* are typically what is transmitted, stored, and compared, however
|
||||||
|
* there may be times when it is necessary to work with the raw
|
||||||
|
* bytes directly, which is why the conversion to a hex string is
|
||||||
|
* a separate step.
|
||||||
|
*/
|
||||||
|
extern char * ShaToHex(unsigned char *);
|
||||||
|
|
||||||
|
#endif /* CYTOPLASM_SHA_H */
|
Loading…
Reference in a new issue