Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
base58.cpp File Reference
#include <string>
#include <vector>
#include <limits>
#include <algorithm>
#include <fc/crypto/base58.hpp>
#include <fc/log/logger.hpp>
#include <fc/string.hpp>
#include <fc/exception/exception.hpp>
#include <stdexcept>
#include <openssl/bn.h>
Include dependency graph for base58.cpp:

Go to the source code of this file.

Classes

class  bignum_error
 
class  CAutoBN_CTX
 
class  CBigNum
 

Namespaces

namespace  fc
 namespace sysio::chain
 

Macros

#define BITCOIN_BASE58_H
 

Functions

const CBigNum operator+ (const CBigNum &a, const CBigNum &b)
 
const CBigNum operator- (const CBigNum &a, const CBigNum &b)
 
const CBigNum operator- (const CBigNum &a)
 
const CBigNum operator* (const CBigNum &a, const CBigNum &b)
 
const CBigNum operator/ (const CBigNum &a, const CBigNum &b)
 
const CBigNum operator% (const CBigNum &a, const CBigNum &b)
 
const CBigNum operator<< (const CBigNum &a, unsigned int shift)
 
const CBigNum operator>> (const CBigNum &a, unsigned int shift)
 
bool operator== (const CBigNum &a, const CBigNum &b)
 
bool operator!= (const CBigNum &a, const CBigNum &b)
 
bool operator<= (const CBigNum &a, const CBigNum &b)
 
bool operator>= (const CBigNum &a, const CBigNum &b)
 
bool operator< (const CBigNum &a, const CBigNum &b)
 
bool operator> (const CBigNum &a, const CBigNum &b)
 
std::string EncodeBase58 (const unsigned char *pbegin, const unsigned char *pend, const fc::yield_function_t &yield)
 
std::string EncodeBase58 (const std::vector< unsigned char > &vch, const fc::yield_function_t &yield)
 
bool DecodeBase58 (const char *psz, std::vector< unsigned char > &vchRet)
 
bool DecodeBase58 (const std::string &str, std::vector< unsigned char > &vchRet)
 
std::string fc::to_base58 (const char *d, size_t s, const fc::yield_function_t &yield)
 
std::string fc::to_base58 (const std::vector< char > &data, const fc::yield_function_t &yield)
 
std::vector< char > fc::from_base58 (const std::string &base58_str)
 
size_t fc::from_base58 (const std::string &base58_str, char *out_data, size_t out_data_len)
 

Macro Definition Documentation

◆ BITCOIN_BASE58_H

#define BITCOIN_BASE58_H

Definition at line 16 of file base58.cpp.

Function Documentation

◆ DecodeBase58() [1/2]

bool DecodeBase58 ( const char * psz,
std::vector< unsigned char > & vchRet )
inline

Definition at line 560 of file base58.cpp.

561{
562 CAutoBN_CTX pctx;
563 vchRet.clear();
564 CBigNum bn58 = 58;
565 CBigNum bn = 0;
566 CBigNum bnChar;
567 while (isspace(*psz))
568 psz++;
569
570 // Convert big endian string to bignum
571 for (const char* p = psz; *p; p++)
572 {
573 const char* p1 = strchr(pszBase58, *p);
574 if (p1 == NULL)
575 {
576 while (isspace(*p))
577 p++;
578 if (*p != '\0') {
579 //slog( "%s '%c'", pszBase58,*p );
580 return false;
581 }
582 break;
583 }
584 bnChar.setulong(p1 - pszBase58);
585 if (!BN_mul(bn.to_bignum(), bn.to_bignum(), bn58.to_bignum(), pctx))
586 throw bignum_error("DecodeBase58 : BN_mul failed");
587 bn += bnChar;
588 }
589
590 // Get bignum as little endian data
591 std::vector<unsigned char> vchTmp = bn.getvch();
592
593 // Trim off sign byte if present
594 if (vchTmp.size() >= 2 && vchTmp.end()[-1] == 0 && vchTmp.end()[-2] >= 0x80)
595 vchTmp.erase(vchTmp.end()-1);
596
597 // Restore leading zeros
598 int nLeadingZeros = 0;
599 for (const char* p = psz; *p == pszBase58[0]; p++)
600 nLeadingZeros++;
601 vchRet.assign(nLeadingZeros + vchTmp.size(), 0);
602
603 // Convert little endian data to big endian
604 reverse_copy(vchTmp.begin(), vchTmp.end(), vchRet.end() - vchTmp.size());
605 return true;
606}
const mie::Vuint & p
Definition bn.cpp:27
const BIGNUM * to_bignum() const
Definition base58.cpp:419
void setulong(unsigned long n)
Definition base58.cpp:114
Definition bn.h:56
Here is the call graph for this function:
Here is the caller graph for this function:

◆ DecodeBase58() [2/2]

bool DecodeBase58 ( const std::string & str,
std::vector< unsigned char > & vchRet )
inline

Definition at line 610 of file base58.cpp.

611{
612 return DecodeBase58(str.c_str(), vchRet);
613}
bool DecodeBase58(const char *psz, std::vector< unsigned char > &vchRet)
Definition base58.cpp:560
return str
Definition CLI11.hpp:1359
Here is the call graph for this function:

◆ EncodeBase58() [1/2]

std::string EncodeBase58 ( const std::vector< unsigned char > & vch,
const fc::yield_function_t & yield )
inline

Definition at line 553 of file base58.cpp.

554{
555 return EncodeBase58(&vch[0], &vch[0] + vch.size(), yield);
556}
std::string EncodeBase58(const unsigned char *pbegin, const unsigned char *pend, const fc::yield_function_t &yield)
Definition base58.cpp:505
Here is the call graph for this function:

◆ EncodeBase58() [2/2]

std::string EncodeBase58 ( const unsigned char * pbegin,
const unsigned char * pend,
const fc::yield_function_t & yield )
inline

Definition at line 505 of file base58.cpp.

506{
507 CAutoBN_CTX pctx;
508 CBigNum bn58 = 58;
509 CBigNum bn0 = 0;
510
511 // Convert big endian data to little endian
512 // Extra zero at the end make sure bignum will interpret as a positive number
513 std::vector<unsigned char> vchTmp(pend-pbegin+1, 0);
514 yield();
515 reverse_copy(pbegin, pend, vchTmp.begin());
516 yield();
517
518 // Convert little endian data to bignum
519 CBigNum bn;
520 bn.setvch(vchTmp);
521
522 // Convert bignum to std::string
523 std::string str;
524 // Expected size increase from base58 conversion is approximately 137%
525 // use 138% to be safe
526 str.reserve((pend - pbegin) * 138 / 100 + 1);
527 CBigNum dv;
528 CBigNum rem;
529 while (bn > bn0)
530 {
531 yield();
532 if (!BN_div(dv.to_bignum(), rem.to_bignum(), bn.to_bignum(), bn58.to_bignum(), pctx))
533 throw bignum_error("EncodeBase58 : BN_div failed");
534 bn = dv;
535 unsigned int c = rem.getulong();
536 str += pszBase58[c];
537 }
538
539 // Leading zeroes encoded as base58 zeros
540 for (const unsigned char* p = pbegin; p < pend && *p == 0; p++)
541 str += pszBase58[0];
542
543 yield();
544 // Convert little endian std::string to big endian
545 reverse(str.begin(), str.end());
546// slog( "Encode '%s'", str.c_str() );
547 yield();
548
549 return str;
550}
unsigned long getulong() const
Definition base58.cpp:120
thread_local yield_t yield
Definition yield.hpp:52
Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator!=()

bool operator!= ( const CBigNum & a,
const CBigNum & b )
inline

Definition at line 495 of file base58.cpp.

495{ return (BN_cmp(a.to_bignum(), b.to_bignum()) != 0); }
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
Here is the call graph for this function:

◆ operator%()

const CBigNum operator% ( const CBigNum & a,
const CBigNum & b )
inline

Definition at line 470 of file base58.cpp.

471{
472 CAutoBN_CTX pctx;
473 CBigNum r;
474 if (!BN_mod(r.to_bignum(), a.to_bignum(), b.to_bignum(), pctx))
475 throw bignum_error("CBigNum::operator% : BN_div failed");
476 return r;
477}
const mie::Vuint & r
Definition bn.cpp:28
Here is the call graph for this function:

◆ operator*()

const CBigNum operator* ( const CBigNum & a,
const CBigNum & b )
inline

Definition at line 452 of file base58.cpp.

453{
454 CAutoBN_CTX pctx;
455 CBigNum r;
456 if (!BN_mul(r.to_bignum(), a.to_bignum(), b.to_bignum(), pctx))
457 throw bignum_error("CBigNum::operator* : BN_mul failed");
458 return r;
459}
Here is the call graph for this function:

◆ operator+()

const CBigNum operator+ ( const CBigNum & a,
const CBigNum & b )
inline

Definition at line 429 of file base58.cpp.

430{
431 CBigNum r;
432 if (!BN_add(r.to_bignum(), a.to_bignum(), b.to_bignum()))
433 throw bignum_error("CBigNum::operator+ : BN_add failed");
434 return r;
435}
Here is the call graph for this function:

◆ operator-() [1/2]

const CBigNum operator- ( const CBigNum & a)
inline

Definition at line 445 of file base58.cpp.

446{
447 CBigNum r(a);
448 BN_set_negative(r.to_bignum(), !BN_is_negative(r.to_bignum()));
449 return r;
450}

◆ operator-() [2/2]

const CBigNum operator- ( const CBigNum & a,
const CBigNum & b )
inline

Definition at line 437 of file base58.cpp.

438{
439 CBigNum r;
440 if (!BN_sub(r.to_bignum(), a.to_bignum(), b.to_bignum()))
441 throw bignum_error("CBigNum::operator- : BN_sub failed");
442 return r;
443}
Here is the call graph for this function:

◆ operator/()

const CBigNum operator/ ( const CBigNum & a,
const CBigNum & b )
inline

Definition at line 461 of file base58.cpp.

462{
463 CAutoBN_CTX pctx;
464 CBigNum r;
465 if (!BN_div(r.to_bignum(), NULL, a.to_bignum(), b.to_bignum(), pctx))
466 throw bignum_error("CBigNum::operator/ : BN_div failed");
467 return r;
468}
Here is the call graph for this function:

◆ operator<()

bool operator< ( const CBigNum & a,
const CBigNum & b )
inline

Definition at line 498 of file base58.cpp.

498{ return (BN_cmp(a.to_bignum(), b.to_bignum()) < 0); }
Here is the call graph for this function:

◆ operator<<()

const CBigNum operator<< ( const CBigNum & a,
unsigned int shift )
inline

Definition at line 479 of file base58.cpp.

480{
481 CBigNum r;
482 if (!BN_lshift(r.to_bignum(), a.to_bignum(), shift))
483 throw bignum_error("CBigNum:operator<< : BN_lshift failed");
484 return r;
485}

◆ operator<=()

bool operator<= ( const CBigNum & a,
const CBigNum & b )
inline

Definition at line 496 of file base58.cpp.

496{ return (BN_cmp(a.to_bignum(), b.to_bignum()) <= 0); }
Here is the call graph for this function:

◆ operator==()

bool operator== ( const CBigNum & a,
const CBigNum & b )
inline

Definition at line 494 of file base58.cpp.

494{ return (BN_cmp(a.to_bignum(), b.to_bignum()) == 0); }
Here is the call graph for this function:

◆ operator>()

bool operator> ( const CBigNum & a,
const CBigNum & b )
inline

Definition at line 499 of file base58.cpp.

499{ return (BN_cmp(a.to_bignum(), b.to_bignum()) > 0); }
Here is the call graph for this function:

◆ operator>=()

bool operator>= ( const CBigNum & a,
const CBigNum & b )
inline

Definition at line 497 of file base58.cpp.

497{ return (BN_cmp(a.to_bignum(), b.to_bignum()) >= 0); }
Here is the call graph for this function:

◆ operator>>()

const CBigNum operator>> ( const CBigNum & a,
unsigned int shift )
inline

Definition at line 487 of file base58.cpp.

488{
489 CBigNum r = a;
490 r >>= shift;
491 return r;
492}