int ejoy_decrypt_auth_token(const char* token, unsigned int token_sz, const char* svr_pass, unsigned int svr_pass_sz, const char* svr_sig, unsigned int svr_sig_sz, struct ejoy_token* t) { if (token_sz != 160) { //token长度无效 return 1; } char ts[128]; memcpy(ts, token, 128); char tss[16]; fromhex(token+128, tss, 16); MD5_CTX md5; MD5Init(&md5); char decrypt[16]; MD5Update(&md5, (unsigned char*)ts, 128); MD5Update(&md5, (unsigned char*)svr_sig, svr_sig_sz); MD5Final(&md5, (unsigned char*)decrypt); for (int i = 0; i < 16; i++) { if (tss[i] != decrypt[i]) { //token验证失败 return 1; } } char tsss[64]; fromhex(token, tsss, 64); CRijndael oRijndael; oRijndael.MakeKey((char*)svr_pass, NULL, svr_pass_sz, svr_pass_sz); char ret[64]; oRijndael.Decrypt(tsss, ret, 64, CRijndael::ECB); char temp[3][64] = {0}; int index = 0; int pos = 0; for (int i = 0; i < 64; i++) { if (ret[i] == '\n') { index++; pos = 0; } else { temp[index][pos++] = ret[i]; } } //token验证成功 memcpy(t->uid, temp[0], sizeof(t->uid)); t->timestamp = atol(temp[1]); t->randomkey = atol(temp[2]); return 0; }
static int lexhexstring(fz_stream *f, char *buf, int n) { char *s = buf; char *e = buf + n; int a = 0, x = 0; int c; while (s < e) { c = fz_readbyte(f); switch (c) { case ISWHITE: break; case ISHEX: if (x) { *s++ = a * 16 + fromhex(c); x = !x; } else { a = fromhex(c); x = !x; } break; case '>': default: goto end; } } end: return s - buf; }
static char * get_spec_by_uuid(const char *s, int *major, int *minor) { unsigned char uuid[16]; int i; if (strlen(s) != 36 || s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' ) { goto bad_uuid; } for (i = 0; i < 16; i++) { if (*s == '-') s++; if (!isxdigit(s[0]) || !isxdigit(s[1])) goto bad_uuid; uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1])); s += 2; } return get_spec_by_x(UUID, (char *)uuid, major, minor); bad_uuid: fprintf(stderr, _("mount: bad UUID")); return 0; }
static void lexname(fz_stream *f, unsigned char *s, int n) { unsigned char *p = s; unsigned char *q = s; while (n > 1) { if (!isregular(fz_peekbyte(f))) break; *s++ = fz_readbyte(f); n--; } *s = '\0'; while (*p) { if (p[0] == '#' && p[1] != 0 && p[2] != 0) { *q++ = fromhex(p[1]) * 16 + fromhex(p[2]); p += 3; } else *q++ = *p++; } *q = '\0'; }
void moritz_func(char *in) { if(in[1] == 'r') { // Reception on rf_moritz_init(); } else if(in[1] == 's' || in[1] == 'f' ) { // Send/Send fast uint8_t dec[MAX_MORITZ_MSG]; uint8_t hblen = fromhex(in+2, dec, MAX_MORITZ_MSG-1); if ((hblen-1) != dec[0]) { DS_P(PSTR("LENERR\r\n")); return; } moritz_sendraw(dec, in[1] == 's'); } else if(in[1] == 'a') { // Auto-Ack fromhex(in+2, autoAckAddr, 3); } else if(in[1] == 'w') { // Fake Wall-Thermostat fromhex(in+2, fakeWallThermostatAddr, 3); } else { // Off moritz_on = 0; } }
/* * rfc1738_unescape() - Converts escaped characters (%xy numbers) in * given the string. %% is a %. %ab is the 8-bit hexadecimal number "ab" */ void rfc1738_unescape(char *s) { int i, j; /* i is write, j is read */ for (i = j = 0; s[j]; i++, j++) { s[i] = s[j]; if (s[j] != '%') { /* normal case, nothing more to do */ } else if (s[j + 1] == '%') { /* %% case */ j++; /* Skip % */ } else { /* decode */ int v1, v2, x; v1 = fromhex(s[j + 1]); if (v1 < 0) continue; /* non-hex or \0 */ v2 = fromhex(s[j + 2]); if (v2 < 0) continue; /* non-hex or \0 */ x = v1 << 4 | v2; if (x > 0 && x <= 255) { s[i] = x; j += 2; } } } s[i] = '\0'; }
static int lexhexstring(fz_stream *f, unsigned char *buf, int n) { unsigned char *s = buf; unsigned char *e = buf + n; int a = 0, x = 0; int c; while (s < e) { c = fz_readbyte(f); if (c == '>') break; else if (iswhite(c)) continue; else if (ishex(c)) { if (x) { *s++ = a * 16 + fromhex(c); x = !x; } else { a = fromhex(c); x = !x; } } else break; } return s - buf; }
END_TEST START_TEST(test_bip32_compare) { HDNode node1, node2, node3; int i, r; hdnode_from_seed(fromhex("301133282ad079cbeb59bc446ad39d333928f74c46997d3609cd3e2801ca69d62788f9f174429946ff4e9be89f67c22fae28cb296a9b37734f75e73d1477af19"), 64, &node1); hdnode_from_seed(fromhex("301133282ad079cbeb59bc446ad39d333928f74c46997d3609cd3e2801ca69d62788f9f174429946ff4e9be89f67c22fae28cb296a9b37734f75e73d1477af19"), 64, &node2); for (i = 0; i < 300; i++) { memcpy(&node3, &node1, sizeof(HDNode)); r = hdnode_private_ckd(&node1, i); ck_assert_int_eq(r, 1); r = hdnode_public_ckd(&node2, i); ck_assert_int_eq(r, 1); r = hdnode_public_ckd(&node3, i); ck_assert_int_eq(r, 1); ck_assert_int_eq(node1.depth, node2.depth); ck_assert_int_eq(node1.depth, node3.depth); ck_assert_int_eq(node1.fingerprint, node2.fingerprint); ck_assert_int_eq(node1.fingerprint, node3.fingerprint); ck_assert_int_eq(node1.child_num, node2.child_num); ck_assert_int_eq(node1.child_num, node3.child_num); ck_assert_mem_eq(node1.chain_code, node2.chain_code, 32); ck_assert_mem_eq(node1.chain_code, node3.chain_code, 32); ck_assert_mem_eq(node2.private_key, fromhex("0000000000000000000000000000000000000000000000000000000000000000"), 32); ck_assert_mem_eq(node3.private_key, fromhex("0000000000000000000000000000000000000000000000000000000000000000"), 32); ck_assert_mem_eq(node1.public_key, node2.public_key, 33); ck_assert_mem_eq(node1.public_key, node3.public_key, 33); } }
//-------------------------------------------------------------------- void ccreg(char *in) { uint8_t hb, out, addr; if(in[1] == 'w' && fromhex(in+2, &addr, 1) && fromhex(in+4, &hb, 1)) { cc1100_writeReg(addr, hb); ccStrobe( CC1100_SCAL ); ccRX(); DH2(addr); DH2(hb); DNL(); } else if(fromhex(in+1, &hb, 1)) { if(hb == 0x99) { for(uint8_t i = 0; i < 0x30; i++) { DH2(cc1100_readReg(i)); if((i&7) == 7) DNL(); } } else { out = cc1100_readReg(hb); DC('C'); // prefix DH2(hb); // register number DS_P( PSTR(" = ") ); DH2(out); // result, hex DS_P( PSTR(" / ") ); DU(out,2); // result, decimal DNL(); } } }
void dmx_func(char *in) { uint8_t hb[4], d = 0; if(in[1] == 'r') { // print latest record memset( hb, 0, sizeof(hb) ); d = fromhex(in+2, hb, 1); if(d) { DH2( dmx_get_level( hb[0] )); DNL(); } } else if(in[1] == 'w') { memset( hb, 0, sizeof(hb) ); d = fromhex(in+2, hb, 2); if(d == 2) { dmx_set_level( hb[0], hb[1] ); } if(d > 0) { DH2( dmx_get_level( hb[0] )); DNL(); } } else if(in[1] == 'c') { DU( channel_count, 3 ); DNL(); } }
END_TEST START_TEST(test_sign_speed) { uint8_t sig[64], priv_key[32], msg[256]; size_t i; int res; for (i = 0; i < sizeof(msg); i++) { msg[i] = i * 1103515245; } clock_t t = clock(); memcpy(priv_key, fromhex("c55ece858b0ddd5263f96810fe14437cd3b5e1fbd7c6a2ec1e031f05e86d8bd5"), 32); for (i = 0 ; i < 250; i++) { res = ecdsa_sign(priv_key, msg, sizeof(msg), sig); ck_assert_int_eq(res, 0); } memcpy(priv_key, fromhex("509a0382ff5da48e402967a671bdcde70046d07f0df52cff12e8e3883b426a0a"), 32); for (i = 0 ; i < 250; i++) { res = ecdsa_sign(priv_key, msg, sizeof(msg), sig); ck_assert_int_eq(res, 0); } printf("Signing speed: %0.2f sig/s\n", 500.0f / ((float)(clock() - t) / CLOCKS_PER_SEC)); }
static int logchar (FILE *fp) { int ch; int ch2; ch = fgetc (fp); fputc (ch, stdout); fflush (stdout); switch (ch) { case '\n': ch = EOL; break; case '\\': ch = fgetc (fp); fputc (ch, stdout); fflush (stdout); switch (ch) { case '\\': break; case 'b': ch = '\b'; break; case 'f': ch = '\f'; break; case 'n': ch = '\n'; break; case 'r': ch = '\r'; break; case 't': ch = '\t'; break; case 'v': ch = '\v'; break; case 'x': ch2 = fgetc (fp); fputc (ch2, stdout); fflush (stdout); ch = fromhex (ch2) << 4; ch2 = fgetc (fp); fputc (ch2, stdout); fflush (stdout); ch |= fromhex (ch2); break; default: /* Treat any other char as just itself */ break; } default: break; } return (ch); }
static void hextomem(uint8_t *mem, const char *buf, int len) { int i; for(i = 0; i < len; i++) { mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]); buf += 2; } }
int NonBlockInput::doDecode(char* src, char* dev, int* sub, byte* dest) { int ret; int count; int len; bool isHex; // HEX->Binary conversion char* start = src; byte cs = 0; byte cs2; int i; *dev++ = *src++; *dev++ = *src++; *dev = '\0'; if (' ' != *src++) return -1; *sub = *src++ - '0'; if (' ' != *src++) return -2; isHex = ('-' == *src); if (isHex) src++; len = atoi(src); count = 1; if (len > 9) count = 2; if (len > 99) count = 3; if (len > 999) count = 4; src += count; if (' ' != *src++) return -3; if (isHex) { count++; // allow for '-' int b; for (i=0; i<len; i++) { if (-1 == (b = fromhex(src))) return -4; *dest++ = b; src+=2; if (' ' != *src++) return -5; } ret = len; count += 3+2+(3*len); } else { for (i=0; i<len; i++) *dest++ = *src++; if (' ' != *src++) return -7; ret = len; count += 3+2+1+len; } for (i=0; i<count; i++) cs += start[i]; cs2 = fromhex(src); if (0 != (0xFF & (cs + cs2))) { Serial.print("doDecode: "); Serial.print(cs, HEX); Serial.print(" + "); Serial.print(cs2, HEX); Serial.print(" = "); Serial.println(cs+cs2, HEX); return -8; } return ret; }
int main() { char test[] = "deadbeef"; char test2[] = "0xcafebabe"; char test3[] = "yo mama"; printf("%s = %lu\n%s = %lu\n%s = %lu\n", test, fromhex(test), test2, fromhex(test2), test3, fromhex(test3) ); return 0; }
void write_eeprom(char *in) { uint8_t hb[6], d = 0; #ifdef HAS_ETHERNET if(in[1] == 'i') { uint8_t *addr = 0; if(in[2] == 'm') { d=6; fromhex(in+3,hb,6); addr=EE_MAC_ADDR; } else if(in[2] == 'd') { d=1; fromdec(in+3,hb); addr=EE_USE_DHCP; } else if(in[2] == 'a') { d=4; fromip (in+3,hb,4); addr=EE_IP4_ADDR; } else if(in[2] == 'n') { d=4; fromip (in+3,hb,4); addr=EE_IP4_NETMASK; } else if(in[2] == 'g') { d=4; fromip (in+3,hb,4); addr=EE_IP4_GATEWAY; } else if(in[2] == 'p') { d=2; fromdec(in+3,hb); addr=EE_IP4_TCPLINK_PORT; } else if(in[2] == 'N') { d=4; fromip (in+3,hb,4); addr=EE_IP4_NTPSERVER; } else if(in[2] == 'o') { d=1; fromhex(in+3,hb,1); addr=EE_IP4_NTPOFFSET; #ifdef HAS_NTP extern int8_t ntp_gmtoff; ntp_gmtoff = hb[0]; #endif } for(uint8_t i = 0; i < d; i++) ewb(addr++, hb[i]); } else #endif { uint16_t addr; d = fromhex(in+1, hb, 3); if(d < 2) return; if(d == 2) addr = hb[0]; else addr = (hb[0] << 8) | hb[1]; ewb((uint8_t*)addr, hb[d-1]); if (addr == 15 || addr == 16 || addr == 17) checkFrequency(); // If there are still bytes left, then write them too in += (2*d+1); while(in[0]) { addr++; if(!fromhex(in, hb, 1)) return; ewb((uint8_t*)addr++, hb[0]); in += 2; } } }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------ void CCertUtils::ConvHex (BYTE *pIn, DWORD dwLen, BYTE *pOut ) { #define fromhex(x) (x-((x>='0')&&(x<='9')?'0':((x>='A')&&(x<='F')?'7':'W'))) register DWORD i; for(i=0; i < dwLen; i+=2) { pOut[ i / 2 ] = ( BYTE )( ( fromhex( pIn[ i ] ) << 4 ) + fromhex( pIn[ i + 1 ] ) ); } }
unsigned int scan_ip6_flat(const char *s,char ip[16]) { int i; for (i=0; i<16; i++) { int tmp; tmp=fromhex(*s++); if (tmp<0) return 0; ip[i]=tmp << 4; tmp=fromhex(*s++); if (tmp<0) return 0; ip[i]+=tmp; } return 32; }
ssize_t scan_urlencoded_query(char **string, char *deststring, SCAN_SEARCHPATH_FLAG flags) { const unsigned char* s=*(const unsigned char**) string; unsigned char *d = (unsigned char*)deststring; unsigned char b, c, f; /* This is the main decoding loop. 'flag' determines, which characters are non-terminating in current context (ie. stop at '=' and '&' if scanning for a 'param'; stop at '?' if scanning for the path ) */ while( ( f = is_unreserved[ c = *s++ ] ) & flags ) { /* When encountering an url escaped character, try to decode */ if( c=='%') { if( ( b = fromhex(*s++) ) == 0xff ) return -1; if( ( c = fromhex(*s++) ) == 0xff ) return -1; c|=(b<<4); } /* Write (possibly decoded) character to output */ *d++ = c; } switch( c ) { case 0: case '\r': case '\n': case ' ': /* If we started scanning on a hard terminator, indicate we've finished */ if( d == (unsigned char*)deststring ) return -2; /* Else make the next call to scan_urlencoded_param encounter it again */ --s; break; case '?': /* XXX to help us parse path?param=value?param=value?... sent by µTorrent 1600 do not return an error but silently terminate if( flags != SCAN_PATH ) return -1; */ break; case '=': if( flags != SCAN_SEARCHPATH_PARAM ) return -1; break; case '&': if( flags == SCAN_PATH ) return -1; if( flags == SCAN_SEARCHPATH_PARAM ) --s; break; default: return -1; } *string = (char *)s; return d - (unsigned char*)deststring; }
///////////////////////////////////////////////////////////////////// // First byte, hex: Row number if less then 32, Display text in title else // Rest: Text to display, up to 16 char. // 00:title (big font). // 01-08:body // 09:scroll up, add row at bottom // 0A:scroll down, add row at top // 0B:redraw screen. // FF:control functions: Switch LCD on/of, set contrast void lcdfunc(char *in) { uint8_t hb[4]; uint8_t narg = fromhex(in+1, hb, 4); if(narg > 0 && hb[0] == 0xFF) { if(hb[1] != 0xFF) lcd_switch(hb[1]); if(hb[2] != 0xFF) lcd_contrast(hb[2]); #ifdef LCD_BL_PWM if(hb[3] != 0xFF) lcd_brightness(hb[3]); #endif return; } if(narg == 0 || hb[0] >= 0x20) { // Strange lines go to the title hb[0] = 0; in += 1; } else { in += 3; } lcd_putline(hb[0], in); }
static char *hex2strn(const char *data, const int len) { if (len == 0) { return NULL; } int i, j; int result_len = len/2 + 1; char *result = (char *)malloc((result_len + 1) * sizeof(char)); for (i=0, j=0; i<len; i+=2) { result[j++] = fromhex(data[i]) << 4 | fromhex(data[i+1]); } result[j] = 0x00; return result; }
//-------------------------------------------------------------------- void ccreg(char *in) { uint8_t hb, out; if(fromhex(in+1, &hb, 1)) { if(hb == 0x99) { for(uint8_t i = 0; i < 0x30; i++) { DH2(cc1100_readReg(i)); if((i&7) == 7) DNL(); } } else { out = cc1100_readReg(hb); DC('C'); // prefix DH2(hb); // register number DS_P( PSTR(" = ") ); DH2(out); // result, hex DS_P( PSTR(" / ") ); DU(out,2); // result, decimal DNL(); } } }
// LED void ledfunc(char *in) { fromhex(in+1, &led_mode, 1); #ifdef XLED switch (led_mode) { case 0: xled_pattern = 0; break; case 1: xled_pattern = 0xffff; break; case 2: xled_pattern = 0xff00; break; case 3: xled_pattern = 0xa000; break; case 4: xled_pattern = 0xaa00; break; } #else if(led_mode & 1) LED_ON(); else LED_OFF(); #endif ewb(EE_LED, led_mode); }
uint8_t is_dcon_frame( uint8_t *cp, uint8_t len ) { if( cp[len-1] != 0x0D ) return 0; switch( *cp ) { case '$': case '#': case '%': case '@': case '*': break; default: return 0; } uint8_t recv_crc = fromhex( cp+len-3 ); uint8_t calc_crc = dcon_checksum( cp, len - 3 ); if( calc_crc != recv_crc ) { modbus_crc_cnt++; return 0; } return 1; }
int dmx_fs20_emu(char *in) { uint8_t hb[4], d = 0; #ifdef DMX_FS20EMU_HC if (!strncmp(in+1, DMX_FS20EMU_HC, 4)) { memset( hb, 0, sizeof(hb) ); d = fromhex(in+5, hb, 2); if(d == 2) { switch (hb[1]) { case 0: break; case 0x1 ... 0xf: hb[1] <<= 4; break; case 0x10: case 0x11: hb[1] = 0xff; break; default: return 1; } dmx_set_level( hb[0], hb[1] ); return 1; } }
static int readahxd(fz_stream *stm, unsigned char *buf, int len) { fz_ahxd *state = stm->state; unsigned char *p = buf; unsigned char *ep = buf + len; int a, b, c, odd; odd = 0; while (p < ep) { if (state->eod) return p - buf; c = fz_readbyte(state->chain); if (c < 0) return p - buf; if (ishex(c)) { if (!odd) { a = fromhex(c); odd = 1; } else { b = fromhex(c); *p++ = (a << 4) | b; odd = 0; } } else if (c == '>') { if (odd) *p++ = (a << 4); state->eod = 1; } else if (!iswhite(c)) { return fz_throw("bad data in ahxd: '%c'", c); } } return p - buf; }
static char *get_path(struct request *req, char **host, char **http) { char *ret, *in, *out; if(!strncmp("http://", req->buf + req->cur, 7)) { char *slash; req->cur += 7; *host = req->buf + req->cur; slash = strpbrk(req->buf + req->cur, "/ \r\n"); if(!slash || *slash != '/') return 0; req->cur += slash - (req->buf + req->cur); } ret = req->buf + req->cur; for(in = out = ret; *in != ' ' && *in != '\r' && *in != '\n' ; in++, out++) { if(*in == '%') { int n; char c = 0; if(!*++in) return 0; if(!in[1]) return 0; n = fromhex(*in); if(n < 0) return 0; c = n << 4; n = fromhex(*++in); if(n < 0) return 0; c |= n; if(c == '/') return 0; *out = c; } else { *out = *in; } } in++; *http = in; for(; *in != '\r' && *in != '\n'; in++); *in = '\0'; if(strcmp(*http, "") == 0) *http = "HTTP/0.9"; req->cur += in - (req->buf + req->cur); req->cur++; if(req->buf[req->cur] == '\n') req->cur++; *out = '\0'; return ret; }
//-------------------------------------------------------------------- void ccsetpa(char *in) { uint8_t hb = 2; fromhex(in+1, &hb, 1); cc_set_pa(hb); ccInitChip(EE_CC1100_CFG); }
int helios_fs20_emu(char *in) { uint8_t hb[4], d = 0; #ifdef HELIOS_EMU_HC if (!strncmp(in+1, HELIOS_EMU_HC, 4)) { memset( hb, 0, sizeof(hb) ); d = fromhex(in+5, hb, 2); if(d == 2) { switch (hb[1]) { case 0: helios_set( 0x29, 0 ); break; case 0x1: case 0x2: helios_set( 0x29, 1 ); break; case 0x3: case 0x4: helios_set( 0x29, 3 ); break; case 0x5: case 0x6: helios_set( 0x29, 7 ); break; case 0x7: case 0x8: helios_set( 0x29, 0xf ); break; case 0x9: case 0xa: helios_set( 0x29, 0x1f ); break; case 0xb: case 0xc: helios_set( 0x29, 0x3f ); break; case 0xd: case 0xe: helios_set( 0x29, 0x7f ); break; case 0xf: case 0x10: case 0x11: helios_set( 0x29, 0xff ); break; default: return 1; } return 1; } } #endif return 0; }
int fDns_read_secret(const char *filename) { // fprintf(stderr, "(%s:%s:%i) filename: '%s'\n", __FILE__, __func__, __LINE__, filename); file = fopen(filename, "r"); if (file == NULL) { fprintf(stderr, "(%s:%s:%i) errno: '%s'\n", __FILE__, __func__, __LINE__, strerror(errno)); return -1; } while ((read = getline(&line, &len, file)) != -1) { sl = strlen(line); // fprintf(stderr, "(%s:%s:%i) '%.*s' sl: %u\n", __FILE__, __func__, __LINE__, sl - 1, line, sl); if (sl > 63) { //check if 64 bytes hex... if (fromhex((const unsigned char *)line, &dnscurve_keys[1 + dnscurve_keys_len].server_secretkey[0])) { rc = crypto_scalarmult_curve25519_base(&dnscurve_keys[1 + dnscurve_keys_len].server_publickey[0], &dnscurve_keys[1 + dnscurve_keys_len].server_secretkey[0]); if (rc != 0) { explicit_bzero((void *)&dnscurve_keys[1 + dnscurve_keys_len].server_secretkey[0], crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES); fprintf(stderr, "(%s:%s:%i) (crypto_scalarmult_curve25519_base) rc: %i\n", __FILE__, __func__, __LINE__, rc); } else { dnscurve_keys[1 + dnscurve_keys_len].hash = fDns_hash_fnv64(&dnscurve_keys[1 + dnscurve_keys_len].server_publickey[0], crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES); printf("Using hash: '%lu' for public key for host: ", dnscurve_keys[1 + dnscurve_keys_len].hash); for (rc = 0; rc < crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES; rc++) { printf("%02x", dnscurve_keys[1 + dnscurve_keys_len].server_publickey[rc]); } printf("\n"); dnscurve_keys_len++; if (dnscurve_keys_len == DNSCURVE_KEYS_SIZE) { fprintf(stderr, "(%s:%s:%i) Reached the limit of '%i' keys.\n", __FILE__, __func__, __LINE__, dnscurve_keys_len); break; } } } } explicit_bzero((void *)line, len); len = 0; if (line != NULL) free(line); line = NULL; } if (dnscurve_keys_len) { //the first read secret key is also the DNScurve key. fDns_dnscurve_the_key = &dnscurve_keys[1]; } fclose(file); return 0; }