bool CBDecodeBase58Checked(CBBigInt * bi, char * str){ if(NOT CBDecodeBase58(bi, str)) { CBLogError("Memory failure in CBDecodeBase58."); return false; } if (bi->length < 4){ CBLogError("The string passed into CBDecodeBase58Checked decoded into data that was too short."); return false; } // Reverse bytes for checksum generation uint8_t * reversed = malloc(bi->length - 4); if (NOT reversed) { CBLogError("Cannot allocate %i bytes of memory in CBDecodeBase58Checked", bi->length - 4); return false; } for (uint8_t x = 4; x < bi->length; x++) reversed[bi->length - 1 - x] = bi->data[x]; // The checksum uses SHA-256, twice, for some reason unknown to man. uint8_t checksum[32]; uint8_t checksum2[32]; CBSha256(reversed, bi->length - 4, checksum); free(reversed); CBSha256(checksum, 32, checksum2); bool ok = true; for (uint8_t x = 0; x < 4; x++) if (checksum2[x] != bi->data[3-x]) ok = false; if (NOT ok){ CBLogError("The data passed to CBDecodeBase58Checked is invalid. Checksum does not match."); return false; } return true; }
int main(int argc, char * argv[]){ bool encode = strcmp(argv[1],"-d"); // Read comma sperated inputs from the second argument char * inputs[100], * comma; inputs[0] = argv[2]; int num = 1; for (; (comma = strchr(argv[2], ',')); num++) { inputs[num] = comma + 1; *comma = '\0'; argv[2] = comma + 1; } for (int x = 0; x < num; x++) { if (encode) { // Convert hex string into bytes and then encode base58 string CBByteArray * bytes = CBNewByteArrayFromHex(inputs[x]); CBByteArrayReverseBytes(bytes); CBBigInt bi = {CBByteArrayGetData(bytes), bytes->length, bytes->length}; char * output = CBEncodeBase58(&bi); puts(output); free(output); CBReleaseObject(bytes); }else{ // Decode base58 string and then produce data as a hex string. CBBigInt bi; CBBigIntAlloc(&bi, strlen(inputs[x]) * 100 / 136); CBDecodeBase58(&bi, inputs[x]); printf("0x"); for (uint8_t x = bi.length; x--;) printf("%02x", bi.data[x]); puts(""); free(bi.data); } } }
int main(){ unsigned int s = (unsigned int)time(NULL); printf("Session = %ui\n",s); srand(s); // Test checked decode CBBigInt bi; CBBigIntAlloc(&bi, 29); CBDecodeBase58Checked(&bi, "1D5A1q5d192j5gYuWiP3CSE5fcaaZxe6E9", onErrorReceived); // Valid printf("END VALID\n"); CBDecodeBase58Checked(&bi, "1qBd3Y9D8HhzA4bYSKgkPw8LsX4wCcbqBX", onErrorReceived); // Invalid // ??? Test for: // c5f88541634fb7bade5f94ff671d1febdcbda116d2da779038ed767989 bi.data[0] = 0xc5; bi.data[1] = 0xf8; bi.data[2] = 0x85; bi.data[3] = 0x41; bi.data[4] = 0x63; bi.data[5] = 0x4f; bi.data[6] = 0xb7; bi.data[7] = 0xba; bi.data[8] = 0xde; bi.data[9] = 0x5f; bi.data[10] = 0x94; bi.data[11] = 0xff; bi.data[12] = 0x67; bi.data[13] = 0x1d; bi.data[14] = 0x1f; bi.data[15] = 0xeb; bi.data[16] = 0xdc; bi.data[17] = 0xbd; bi.data[18] = 0xa1; bi.data[19] = 0x16; bi.data[20] = 0xd2; bi.data[21] = 0xda; bi.data[22] = 0x77; bi.data[23] = 0x90; bi.data[24] = 0x38; bi.data[25] = 0xed; bi.data[26] = 0x76; bi.data[27] = 0x79; bi.data[28] = 0x89; bi.length = 29; char * str = CBEncodeBase58(&bi); printf("%s\n",str); if (strcmp(str, "7EyVQVmCjB3siBN8DdtuG3ws5jW9xsnT25vbt5eU")) { printf("7EyVQVmCjB3siBN8DdtuG3ws5jW9xsnT25vbt5eU FAIL\n"); return 1; } free(str); unsigned char * verify = malloc(29); for (int x = 0; x < 10000; x++) { for (int y = 0; y < 29; y++) { bi.data[y] = rand(); verify[y] = bi.data[y]; } bi.length = 29; printf("0x"); for (int y = 0; y < 29; y++) { printf("%.2x",verify[y]); } str = CBEncodeBase58(&bi); printf(" -> %s -> \n",str); CBDecodeBase58(&bi,str); free(str); printf("0x"); for (int y = 0; y < 29; y++) { printf("%.2x",bi.data[y]); if (bi.data[y] != verify[y]) { printf(" = FAIL\n"); return 1; } } printf(" = OK\n"); } free(bi.data); return 0; }
int main(){ unsigned int s = (unsigned int)time(NULL); printf("Session = %ui\n",s); srand(s); // Test checked decode CBEvents events; events.onErrorReceived = err; CBDependencies d; d.sha256 = sha256; CBDecodeBase58Checked("1D5A1q5d192j5gYuWiP3CSE5fcaaZxe6E9", &events, &d); // Valid printf("END VALID\n"); CBDecodeBase58Checked("1qBd3Y9D8HhzA4bYSKgkPw8LsX4wCcbqBX", &events, &d); // Invalid unsigned char * test = malloc(29); // ??? Test for: // c5f88541634fb7bade5f94ff671d1febdcbda116d2da779038ed767989 // 7EyVQVmCjB3siBN8DdtuG3ws5jW9xsnT25vbt5eU = CORRECT // 7EyVQVmCjB3siBN8DdtuG3ws64y9xsnT25vbt5eU = FAILURE test[0] = 0xc5; test[1] = 0xf8; test[2] = 0x85; test[3] = 0x41; test[4] = 0x63; test[5] = 0x4f; test[6] = 0xb7; test[7] = 0xba; test[8] = 0xde; test[9] = 0x5f; test[10] = 0x94; test[11] = 0xff; test[12] = 0x67; test[13] = 0x1d; test[14] = 0x1f; test[15] = 0xeb; test[16] = 0xdc; test[17] = 0xbd; test[18] = 0xa1; test[19] = 0x16; test[20] = 0xd2; test[21] = 0xda; test[22] = 0x77; test[23] = 0x90; test[24] = 0x38; test[25] = 0xed; test[26] = 0x76; test[27] = 0x79; test[28] = 0x89; char * str = CBEncodeBase58(test,29); printf("%s\n",str); free(str); unsigned char * verify = malloc(29); for (int x = 0; x < 10000; x++) { for (int y = 0; y < 29; y++) { test[y] = rand(); verify[y] = test[y]; } printf("0x"); for (int y = 0; y < 29; y++) { printf("%.2x",verify[y]); } str = CBEncodeBase58(test,29); printf(" -> %s -> \n",str); CBBigInt bi = CBDecodeBase58(str); free(str); printf("0x"); for (int y = 0; y < 29; y++) { printf("%.2x",bi.data[y]); if (bi.data[y] != verify[y]) { printf(" = FAIL\n"); return 1; } } printf(" = OK\n"); free(bi.data); } return 0; }