Browse Source

thread safe libhash

pull/123/head
notgiven688 3 years ago
parent
commit
e0193c1c82
4 changed files with 46 additions and 25 deletions
  1. +1
    -1
      hash_cn/libhash/Makefile
  2. BIN
      hash_cn/libhash/libhash.so
  3. +15
    -21
      hash_cn/libhash/main.c
  4. +30
    -3
      server/Server/Program.cs

+ 1
- 1
hash_cn/libhash/Makefile View File

@ -1,6 +1,6 @@
TARGET = prog
LIBS = -lm
CC = gcc -O3 -shared -fPIC
CC = gcc -O3 -shared -fPIC -pthread
CFLAGS = -g -Wall -std=gnu99 -maes
.PHONY: default all clean

BIN
hash_cn/libhash/libhash.so View File


+ 15
- 21
hash_cn/libhash/main.c View File

@ -1,33 +1,22 @@
#include <stdio.h>
#include <time.h>
#include "slow-hash.h"
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <stdint.h>
char output[200];
#include "slow-hash.h"
char* tohex(unsigned char * in, size_t len)
{
char *ptr = &output[0];
for (size_t i = 0; i < len; i++)
{
ptr += sprintf (ptr, "%02x",in[i]);
}
return &output[0];
}
char* hash_cn(char* hex, int light)
{
size_t len = strlen(hex)/2;
size_t count = 0;
char* output = (char*) malloc((64+1)*sizeof(char));
int len = strlen(hex)/2;
unsigned char val[100], *pos = hex;
unsigned char val[len], *pos = hex;
for(count = 0; count < len; count++) {
for(size_t count = 0; count < len; count++) {
sscanf(pos, "%2hhx", &val[count]);
pos += 2;
}
@ -37,12 +26,17 @@ char* hash_cn(char* hex, int light)
unsigned char hash[32];
cn_slow_hash(&val,len,&hash,light, variant,0);
char *ptr = &output[0];
for (size_t i = 0; i < 32; i++) {
ptr += sprintf (ptr, "%02x",hash[i]);
}
return tohex(hash,32);
return &output[0];
}
int main (void)
void hash_free(void* ptr)
{
return 0;
free(ptr);
}

+ 30
- 3
server/Server/Program.cs View File

@ -77,9 +77,12 @@ namespace Server {
class MainClass {
[DllImport ("libhash.so", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
[DllImport ("libhash.so", CallingConvention = CallingConvention.StdCall)]
static extern IntPtr hash_cn (string hex, int light);
[DllImport ("libhash.so", CallingConvention = CallingConvention.StdCall)]
static extern IntPtr hash_free (IntPtr ptr);
public const string SEP = "<-|->";
public const string RegexIsHex = "^[a-fA-F0-9]+$";
@ -263,16 +266,23 @@ namespace Server {
string parta = blob.Substring (0, 78);
string partb = blob.Substring (86, blob.Length - 86);
lock (hashLocker) {
// hashlib should be thread safe. If you encounter problems
// (mono crashing with sigsev)
// a workaround is to uncomment the lock.
//lock (hashLocker) {
#if (AEON)
IntPtr pStr = hash_cn (parta + nonce + partb, 1);
#else
IntPtr pStr = hash_cn (parta + nonce + partb, 0);
#endif
string ourresult = Marshal.PtrToStringAnsi (pStr);
hash_free(pStr);
if (ourresult != result) return false;
}
//}
}
@ -478,6 +488,7 @@ namespace Server {
try {
IntPtr pStr = hash_cn (testStr, 0);
hashedResult = Marshal.PtrToStringAnsi (pStr);
hash_free(pStr);
} catch (Exception e) {
ex = e;
return false;
@ -492,8 +503,24 @@ namespace Server {
return true;
}
private static void ExcessiveHashTest()
{
Parallel.For(0,10000, (i) =>
{
string testStr = new string ('1', 151) + '3';
IntPtr ptr = hash_cn (testStr, 0);
string str = Marshal.PtrToStringAnsi (ptr);
hash_free(ptr);
Console.WriteLine(i.ToString() + " " + str);
});
}
public static void Main (string[] args) {
//ExcessiveHashTest(); return;
CConsole.ColorInfo (() => {
#if (DEBUG)

Loading…
Cancel
Save