Skip to content

Commit eb33f4e

Browse files
committed
adding inline to base58
1 parent 39d75fb commit eb33f4e

2 files changed

Lines changed: 16 additions & 84 deletions

File tree

include/nodepp/crypto.h

Lines changed: 10 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -415,58 +415,24 @@ namespace nodepp { class decrypt_t {
415415
namespace nodepp { class base58_encoder_t {
416416
protected:
417417

418-
struct NODE {
419-
queue_t<string_t> bff;
420-
BIGNUM* bn = nullptr; bool state =0;
421-
~NODE() { if( bn ){ BN_clear_free( bn ); } }
422-
}; ptr_t<NODE> obj;
423-
424-
string_t encode( string_t msg ) const noexcept {
425-
if( msg.empty() ){ return nullptr; }
426-
427-
BN_zero(obj->bn); BN_bin2bn((uchar*)msg.data(), msg.size(), obj->bn);
428-
auto chr = string_t( NODEPP_BASE58 );
429-
430-
string_t result; while(!BN_is_zero(obj->bn)) {
431-
int rem = BN_div_word(obj->bn, chr.size());
432-
result.unshift(chr[rem]);
433-
}
434-
435-
for( auto& byte : msg ) {
436-
if ( byte != 0x00 ){ break; }
437-
result.unshift(chr[0]);
438-
}
439-
440-
if( !onData.empty() ){ onData.emit(result); }
441-
442-
return result; }
418+
struct NODE { queue_t<string_t> bff; bool state=0; }; ptr_t<NODE> obj;
443419

444420
public:
445-
446-
event_t<string_t> onData;
447-
event_t<> onClose;
448-
449-
base58_encoder_t() : obj( new NODE() ) {
450-
NODEPP_CRYPTO_INITIALIZATOR();
451-
obj->state = 1; obj->bn = (BIGNUM*) BN_new();
452-
if( !obj->bn ){ NODEPP_THROW_ERROR("can't initializate encoder"); }
453-
}
454-
421+
455422
~base58_encoder_t() noexcept { if( obj.count()>1 ){ return; } free(); }
423+
base58_encoder_t() : obj( new NODE() ) { obj->state = 1; }
456424

457425
string_t get() const noexcept { if( obj->state == 0 ){ return nullptr; }
458-
auto raw = array_t<string_t>( obj->bff.data() ).join(nullptr);
459-
auto data= encode( raw ); free(); return data;
426+
auto raw = string::join( obj->bff, "" );
427+
auto data= encoder::base58::atob( raw ); free(); return data;
460428
}
461429

462430
void update( const string_t& msg ) const noexcept {
463431
if( obj->state!=1 ){ return; } obj->bff.push( msg );
464432
}
465433

466434
void free() const noexcept {
467-
if( obj->state == 1 ){ return; }
468-
obj->state = 0; onClose.emit ();
469-
onData.clear(); onClose.clear();
435+
if( obj->state == 1 ){ return; } obj->state = 0;
470436
}
471437

472438
bool is_available() const noexcept { return obj->state == 1; }
@@ -482,54 +448,20 @@ namespace nodepp { class base58_encoder_t {
482448
namespace nodepp { class base58_decoder_t {
483449
protected:
484450

485-
struct NODE {
486-
queue_t<string_t> bff;
487-
BIGNUM* bn =nullptr; bool state=0;
488-
~NODE() { if( bn ){ BN_clear_free( bn ); } }
489-
}; ptr_t<NODE> obj;
490-
491-
string_t decode( string_t msg ) const noexcept {
492-
if( msg.empty() ){ return nullptr; }
493-
494-
BN_zero(obj->bn); ulong lz = 0; ulong ch = true;
495-
auto chr = string_t( NODEPP_BASE58 );
496-
497-
for ( const auto& c : msg ){
498-
if ( ch && c == chr[0] ){ lz++; }
499-
else{ ch = false; }
500-
501-
const char* pos = strchr(chr.data(), c);
502-
if( pos == nullptr ){ return nullptr; }
503-
504-
BN_mul_word(obj->bn, chr.size());
505-
BN_add_word(obj->bn, pos- chr.data());
506-
}
507-
508-
int num_bytes = BN_num_bytes(obj->bn);
509-
ptr_t<uchar> tmp ( lz + num_bytes, '\0' );
510-
BN_bn2bin( obj->bn, tmp.data() + lz );
511-
512-
string_t out( (char*)tmp.data(), tmp.size() );
513-
514-
return out; }
451+
struct NODE { queue_t<string_t> bff; bool state=0; }; ptr_t<NODE> obj;
515452

516453
public:
517454

518-
base58_decoder_t() : obj( new NODE() ) {
519-
NODEPP_CRYPTO_INITIALIZATOR();
520-
obj->state = 1; obj->bn = (BIGNUM*) BN_new();
521-
if( !obj->bn ){ NODEPP_THROW_ERROR("can't initializate decoder"); }
522-
}
523-
524455
~base58_decoder_t() noexcept { if( obj.count()>1 ){ return; } free(); }
456+
base58_decoder_t() : obj( new NODE() ) { obj->state = 1; }
525457

526458
void update( const string_t& msg ) const noexcept {
527459
if( obj->state!=1 ){ return; } obj->bff.push( msg );
528460
}
529461

530462
string_t get() const noexcept { if( obj->state == 0 ){ return nullptr; }
531-
auto raw = array_t<string_t>( obj->bff.data() ).join(nullptr);
532-
auto data= decode( raw ); free(); return data;
463+
auto raw = string::join( obj->bff, "" );
464+
auto data= encoder::base58::btoa( raw ); free(); return data;
533465
}
534466

535467
void free() const noexcept {

include/nodepp/encoder.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ namespace nodepp { namespace encoder { namespace base64 {
257257

258258
namespace nodepp { namespace encoder { namespace base58 {
259259

260-
string_t atob( string_t message ){
260+
inline string_t atob( string_t message ){
261261

262262
if( message.empty() ){ return nullptr; }
263263

@@ -286,11 +286,11 @@ namespace nodepp { namespace encoder { namespace base58 {
286286
}
287287

288288
out.insert( out.first(), zrs.data() );
289-
out.push( '\0' ); return out.data();
289+
out.push ( '\0' ); return out.data();
290290

291291
}
292292

293-
string_t btoa( string_t message ){
293+
inline string_t btoa( string_t message ){
294294

295295
if ( message.empty() ){ return nullptr; }
296296
int T[256]; type::fill( T, T+256, -1 );
@@ -318,9 +318,9 @@ namespace nodepp { namespace encoder { namespace base58 {
318318

319319
}
320320

321-
out.insert( nullptr, zrs.data() );
322-
out.insert( nullptr, nmb.reverse().ptr() );
323-
out.push( '\0' ); return out.data();
321+
out.insert( nullptr , nmb.reverse().ptr() );
322+
out.insert( out.first(), zrs.data() );
323+
out.push ( '\0' ); return out.data();
324324
}
325325

326326
}}}

0 commit comments

Comments
 (0)