Skip to content

Commit

Permalink
break out hex functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ryancdotorg committed Aug 29, 2015
1 parent 1d653c1 commit bbec415
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 36 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
HEADERS = bloom.h crack.h hash160.h warpwallet.h
OBJECTS = brainflayer.o bloom.o hex2blf.o warpwallet.o
OBJECTS = brainflayer.o bloom.o hex2blf.o warpwallet.o hex.o
BINARIES = brainflayer hex2blf
LIBS = -lssl -lrt -lcrypto -lz -ldl -lgmp
CFLAGS = -O2
Expand Down Expand Up @@ -31,13 +31,13 @@ warpwallet.o: warpwallet.c scrypt-jane/scrypt-jane.h
%.o: %.c
$(COMPILE) -c $< -o $@

hex2blf: hex2blf.o bloom.o
hex2blf: hex2blf.o hex.o bloom.o
$(COMPILE) -static $^ $(LIBS) -o $@

brainflayer: brainflayer.o bloom.o warpwallet.o secp256k1/.libs/libsecp256k1.a scrypt-jane/scrypt-jane.o
brainflayer: brainflayer.o hex.o bloom.o warpwallet.o secp256k1/.libs/libsecp256k1.a scrypt-jane/scrypt-jane.o
$(COMPILE) -static $^ $(LIBS) -o $@

brainflayer-alt: brainflayer.o bloom.o warpwallet.o secp256k1/.libs/libsecp256k1.a scrypt-jane/scrypt-jane.o
brainflayer-alt: brainflayer.o hex.o bloom.o warpwallet.o brainwalletio.o secp256k1/.libs/libsecp256k1.a scrypt-jane/scrypt-jane.o
$(COMPILE) -static $^ $(LIBS) -o $@

clean:
Expand Down
25 changes: 3 additions & 22 deletions brainflayer.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,14 @@

#include "secp256k1/include/secp256k1.h"

#include "hex.h"
#include "bloom.h"
#include "hash160.h"

#include "warpwallet.h"

static int brainflayer_is_init = 0;

static const unsigned char unhex_tab[80] = {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

static unsigned char hash256[SHA256_DIGEST_LENGTH];
static hash160_t hash160_tmp;
static hash160_t hash160_compr;
Expand Down Expand Up @@ -71,15 +61,6 @@ uint64_t getns() {
return ns;
}

inline static unsigned char * unhex(unsigned char *str, size_t str_sz) {
int i;
for (i = 0; i < str_sz; i += 2) {
unhexed[i>>1] = (unhex_tab[str[i+0]&0x4f] & 0xf0)|
(unhex_tab[str[i+1]&0x4f] & 0x0f);
}
return unhexed;
}

inline static int priv2hash160(unsigned char *priv) {
/* only initialize stuff once */
if (!brainflayer_is_init) {
Expand Down Expand Up @@ -162,11 +143,11 @@ static int pass2hash160(unsigned char *pass, size_t pass_sz) {
}

static int hexpass2hash160(unsigned char *hpass, size_t hpass_sz) {
return pass2hash160(unhex(hpass, hpass_sz), hpass_sz>>1);
return pass2hash160(unhex(hpass, hpass_sz, unhexed, sizeof(unhexed)), hpass_sz>>1);
}

static int hexpriv2hash160(unsigned char *hpriv, size_t hpriv_sz) {
return priv2hash160(unhex(hpriv, hpriv_sz));
return priv2hash160(unhex(hpriv, hpriv_sz, unhexed, sizeof(unhexed)));
}

static unsigned char *warpsalt;
Expand Down
20 changes: 20 additions & 0 deletions hex.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* Copyright (c) 2015 Ryan Castellucci, All Rights Reserved */
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>

#include "hex.h"

unsigned char *
hex(unsigned char *buf, size_t buf_sz,
unsigned char *hexed, size_t hexed_sz) {
int i, j;
--hexed_sz;
for (i = j = 0; i < buf_sz && j < hexed_sz; ++i, j += 2) {
sprintf(hexed+j, "%02x", buf[i]);
}
hexed[j] = 0; // null terminate
return hexed;
}

/* vim: set ts=2 sw=2 et ai si: */
30 changes: 30 additions & 0 deletions hex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* Copyright (c) 2015 Ryan Castellucci, All Rights Reserved */
#ifndef __BRAINFLAYER_HEX_H_
#define __BRAINFLAYER_HEX_H_

static const unsigned char unhex_tab[80] = {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

static inline unsigned char *
unhex(unsigned char *str, size_t str_sz,
unsigned char *unhexed, size_t unhexed_sz) {
int i, j;
for (i = j = 0; i < str_sz && j < unhexed_sz; i += 2, ++j) {
unhexed[j] = (unhex_tab[str[i+0]&0x4f] & 0xf0)|
(unhex_tab[str[i+1]&0x4f] & 0x0f);
}
return unhexed;
}

unsigned char * hex(unsigned char *, size_t, unsigned char *, size_t);

#endif /* __BRAINFLAYER_HEX_H_ */
/* vim: set ts=2 sw=2 et ai si: */
13 changes: 3 additions & 10 deletions hex2blf.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <arpa/inet.h> /* for ntohl/htonl */

#include "hex.h"
#include "bloom.h"
#include "hash160.h"

Expand Down Expand Up @@ -78,16 +79,8 @@ int main(int argc, char **argv) {
stat(hashfile, &sb);
fprintf(stderr, "[*] Loading hash160s from '%s' \033[s 0.0%%", hashfile);
while (getline(&line, &line_sz, f) > 0) {
if (sscanf(line, "%08x%08x%08x%08x%08x", &hash.ul[0],
&hash.ul[1], &hash.ul[2], &hash.ul[3], &hash.ul[4])) {
/* fix the byte order */
hash.ul[0] = htonl(hash.ul[0]);
hash.ul[1] = htonl(hash.ul[1]);
hash.ul[2] = htonl(hash.ul[2]);
hash.ul[3] = htonl(hash.ul[3]);
hash.ul[4] = htonl(hash.ul[4]);
bloom_set_hash160(bloom, hash.ul);
}
unhex(line, strlen(line), hash.uc, sizeof(hash.uc));
bloom_set_hash160(bloom, hash.ul);

if ((++i & 0x3ffff) == 0) {
pct = 100.0 * ftell(f) / sb.st_size;
Expand Down

0 comments on commit bbec415

Please sign in to comment.