Compare commits
No commits in common. "9e1026d893046203090bb2a424780e622a954c05" and "f5ce4f52385ed1f7d80fd4115f56e0db03730591" have entirely different histories.
9e1026d893
...
f5ce4f5238
6 changed files with 18 additions and 65 deletions
|
@ -41,16 +41,6 @@
|
||||||
* due to the lack of a 64-bit integer type, so that hash
|
* due to the lack of a 64-bit integer type, so that hash
|
||||||
* function has been omitted.
|
* function has been omitted.
|
||||||
*/
|
*/
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This is an enum to be used to identify the type of SHA used.
|
|
||||||
*/
|
|
||||||
typedef enum HashType
|
|
||||||
{
|
|
||||||
HASH_SHA1,
|
|
||||||
HASH_SHA256
|
|
||||||
} HashType;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function takes a pointer to a NULL-terminated C string, and
|
* This function takes a pointer to a NULL-terminated C string, and
|
||||||
|
@ -74,20 +64,6 @@ extern unsigned char * Sha256(char *);
|
||||||
*/
|
*/
|
||||||
extern unsigned char * Sha1(char *);
|
extern unsigned char * Sha1(char *);
|
||||||
|
|
||||||
/**
|
|
||||||
* This function behaves just like
|
|
||||||
* .Fn Sha256 ,
|
|
||||||
* except that it allows for a generic byte array, instead of a string.
|
|
||||||
*/
|
|
||||||
extern unsigned char * Sha256Raw(unsigned char *, size_t);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This function behaves just like
|
|
||||||
* .Fn Sha1 ,
|
|
||||||
* except that it allows for a generic byte array, instead of a string.
|
|
||||||
*/
|
|
||||||
extern unsigned char * Sha1Raw(unsigned char *, size_t);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a SHA byte buffer into a hex string. These hex strings
|
* Convert a SHA byte buffer into a hex string. These hex strings
|
||||||
* are typically what is transmitted, stored, and compared, however
|
* are typically what is transmitted, stored, and compared, however
|
||||||
|
@ -95,6 +71,6 @@ extern unsigned char * Sha1Raw(unsigned char *, size_t);
|
||||||
* bytes directly, which is why the conversion to a hex string is
|
* bytes directly, which is why the conversion to a hex string is
|
||||||
* a separate step.
|
* a separate step.
|
||||||
*/
|
*/
|
||||||
extern char * ShaToHex(unsigned char *, HashType);
|
extern char * ShaToHex(unsigned char *);
|
||||||
|
|
||||||
#endif /* CYTOPLASM_SHA_H */
|
#endif /* CYTOPLASM_SHA_H */
|
||||||
|
|
33
src/Sha.c
33
src/Sha.c
|
@ -28,44 +28,21 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
char *
|
char *
|
||||||
ShaToHex(unsigned char *bytes, HashType type)
|
ShaToHex(unsigned char *bytes)
|
||||||
{
|
{
|
||||||
size_t i = 0, size;
|
size_t i = 0;
|
||||||
char *str;
|
char *str = Malloc(((strlen((char *) bytes) * 2) + 1) * sizeof(char));
|
||||||
|
|
||||||
switch (type)
|
|
||||||
{
|
|
||||||
case HASH_SHA1:
|
|
||||||
size = 20;
|
|
||||||
break;
|
|
||||||
case HASH_SHA256:
|
|
||||||
size = 32;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
str = Malloc(((size * 2) + 1) * sizeof(char));
|
|
||||||
if (!str)
|
if (!str)
|
||||||
{
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < size; i++)
|
while (bytes[i] != '\0')
|
||||||
{
|
{
|
||||||
snprintf(str + (2 * i), 3, "%02x", bytes[i]);
|
snprintf(str + (2 * i), 3, "%02x", bytes[i]);
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
unsigned char *
|
|
||||||
Sha256(char *str)
|
|
||||||
{
|
|
||||||
return Sha256Raw((unsigned char *) str, str ? strlen(str) : 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char *
|
|
||||||
Sha1(char *str)
|
|
||||||
{
|
|
||||||
return Sha1Raw((unsigned char *) str, str ? strlen(str) : 0);
|
|
||||||
}
|
|
||||||
|
|
|
@ -29,12 +29,12 @@
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
|
||||||
/* TODO: Verify LibreSSL support later */
|
/* TODO: Verify LibreSSL support later */
|
||||||
#if defined(TLS_IMPL) && (TLS_IMPL == TLS_OPENSSL)
|
#if TLS_IMPL == TLS_OPENSSL
|
||||||
|
|
||||||
#include <openssl/sha.h>
|
#include <openssl/sha.h>
|
||||||
|
|
||||||
unsigned char *
|
unsigned char *
|
||||||
Sha1Raw(unsigned char *str, size_t len)
|
Sha1(char *str)
|
||||||
{
|
{
|
||||||
unsigned char *digest;
|
unsigned char *digest;
|
||||||
if (!str)
|
if (!str)
|
||||||
|
@ -43,7 +43,7 @@ Sha1Raw(unsigned char *str, size_t len)
|
||||||
}
|
}
|
||||||
|
|
||||||
digest = Malloc(20 + 1);
|
digest = Malloc(20 + 1);
|
||||||
SHA1(str, len, digest);
|
SHA1((unsigned char *) str, strlen(str), digest);
|
||||||
digest[20] = '\0';
|
digest[20] = '\0';
|
||||||
return digest;
|
return digest;
|
||||||
}
|
}
|
||||||
|
@ -261,7 +261,7 @@ Sha1Calculate(Sha1Context * ctx, unsigned char *out)
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char *
|
unsigned char *
|
||||||
Sha1Raw(unsigned char *str, size_t len)
|
Sha1(char *str)
|
||||||
{
|
{
|
||||||
Sha1Context ctx;
|
Sha1Context ctx;
|
||||||
unsigned char *out;
|
unsigned char *out;
|
||||||
|
@ -278,7 +278,7 @@ Sha1Raw(unsigned char *str, size_t len)
|
||||||
}
|
}
|
||||||
|
|
||||||
Sha1Init(&ctx);
|
Sha1Init(&ctx);
|
||||||
Sha1Update(&ctx, str, len);
|
Sha1Update(&ctx, str, strlen(str));
|
||||||
Sha1Calculate(&ctx, out);
|
Sha1Calculate(&ctx, out);
|
||||||
|
|
||||||
out[160 / 8] = '\0';
|
out[160 / 8] = '\0';
|
||||||
|
|
|
@ -31,12 +31,12 @@
|
||||||
|
|
||||||
|
|
||||||
/* TODO: Verify LibreSSL support later */
|
/* TODO: Verify LibreSSL support later */
|
||||||
#if defined(TLS_IMPL) && (TLS_IMPL == TLS_OPENSSL)
|
#if TLS_IMPL == TLS_OPENSSL
|
||||||
|
|
||||||
#include <openssl/sha.h>
|
#include <openssl/sha.h>
|
||||||
|
|
||||||
unsigned char *
|
unsigned char *
|
||||||
Sha256Raw(unsigned char *str, size_t len)
|
Sha256(char *str)
|
||||||
{
|
{
|
||||||
unsigned char *digest;
|
unsigned char *digest;
|
||||||
if (!str)
|
if (!str)
|
||||||
|
@ -45,7 +45,7 @@ Sha256Raw(unsigned char *str, size_t len)
|
||||||
}
|
}
|
||||||
|
|
||||||
digest = Malloc(32 + 1);
|
digest = Malloc(32 + 1);
|
||||||
SHA256(str, len, digest);
|
SHA256((unsigned char *) str, strlen(str), digest);
|
||||||
digest[32] = '\0';
|
digest[32] = '\0';
|
||||||
return digest;
|
return digest;
|
||||||
}
|
}
|
||||||
|
@ -192,7 +192,7 @@ Sha256Process(Sha256Context * context, unsigned char *data, size_t length)
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char *
|
unsigned char *
|
||||||
Sha256Raw(unsigned char *str, size_t len)
|
Sha256(char *str)
|
||||||
{
|
{
|
||||||
Sha256Context context;
|
Sha256Context context;
|
||||||
size_t i;
|
size_t i;
|
||||||
|
@ -228,7 +228,7 @@ Sha256Raw(unsigned char *str, size_t len)
|
||||||
context.length = 0;
|
context.length = 0;
|
||||||
memset(context.buffer, 0, 64);
|
memset(context.buffer, 0, 64);
|
||||||
|
|
||||||
Sha256Process(&context, str, len);
|
Sha256Process(&context, (unsigned char *) str, strlen(str));
|
||||||
|
|
||||||
memset(fill, 0, 64);
|
memset(fill, 0, 64);
|
||||||
fill[0] = 0x80;
|
fill[0] = 0x80;
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
*/
|
*/
|
||||||
#include <Tls.h>
|
#include <Tls.h>
|
||||||
|
|
||||||
#if defined(TLS_IMPL) && (TLS_IMPL == TLS_LIBRESSL)
|
#if TLS_IMPL == TLS_LIBRESSL
|
||||||
|
|
||||||
#include <Memory.h>
|
#include <Memory.h>
|
||||||
#include <Log.h>
|
#include <Log.h>
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
*/
|
*/
|
||||||
#include <Tls.h>
|
#include <Tls.h>
|
||||||
|
|
||||||
#if defined(TLS_IMPL) && (TLS_IMPL == TLS_OPENSSL)
|
#if TLS_IMPL == TLS_OPENSSL
|
||||||
|
|
||||||
#include <Memory.h>
|
#include <Memory.h>
|
||||||
#include <Log.h>
|
#include <Log.h>
|
||||||
|
|
Loading…
Reference in a new issue