host/baseconv.c
/** Convert 160 bit hash to a human-readable base 58 Bitcoin address such
  * as "1Dinox3mFw8yykpAZXFGEKeH4VX1Mzbcxe".
  * \param out The human-readable base 58 Bitcoin address will be written
  *            here in the form of a null-terminated string. This should
  *            point to a buffer with space for at least #TEXT_ADDRESS_LENGTH
  *            characters, including the terminating null.
  * \param in The 160 bit hash to convert. This should point to an array of
  *           20 bytes containing the hash in big-endian format (as is
  *           typical for hashes).
  * \param address_version The version/application byte to use in calculating
  *                        the address.
  */
Convert 160 bit hash to a human-readable base 58 Bitcoin address such
as "1Dinox3mFw8yykpAZXFGEKeH4VX1Mzbcxe".
\param out The human-readable base 58 Bitcoin address will be written
here in the form of a null-terminated string. This should
point to a buffer with space for at least #TEXT_ADDRESS_LENGTH
characters, including the terminating null.
\param in The 160 bit hash to convert. This should point to an array of
20 bytes containing the hash in big-endian format (as is
typical for hashes).
\param address_version The version/application byte to use in calculating
the address.
Convert 160 bit hash to a human - readable base 58 Bitcoin address such as " 1Dinox3mFw8yykpAZXFGEKeH4VX1Mzbcxe " . \ param out The human - readable base 58 Bitcoin address will be written here in the form of a null - terminated string . This should point to a buffer with space for at least #TEXT_ADDRESS_LENGTH characters including the terminating null . \ param in The 160 bit hash to convert . This should point to an array of 20 bytes containing the hash in big - endian format ( as is typical for hashes ) . \ param address_version The version / application byte to use in calculating the address .
void hashToAddr(char *out, uint8_t *in, uint8_t address_version)
{
	uint8_t r[25];
	uint8_t op1[26];
	uint8_t temp[26];
	uint8_t index;
	uint8_t i;
	uint8_t j;
	uint8_t leading_zero_bytes;
	uint32_t hs[8];
	sha256BeginTZ(4);
	r[24] = address_version;
	sha256WriteTZ(&(address_version), (uint32_t)1, 4);
	for (i = 0; i < 20; i++)
	{
		r[23 - i] = in[i];
	}
	sha256WriteTZ(in, (uint32_t)20, 4);
	sha256FinishDoubleTZ(hs, (uint32_t)32, 4);
	writeU32LittleEndian(r, hs[0]);
	leading_zero_bytes = 0;
	for (i = 24; i < 25; i--)
	{
		if (r[i] == 0)
		{
			leading_zero_bytes++;
		}
		else
		{
			break;
		}
	}
	index = 34;
	for (i = 0; i < 35; i++)
	{
		memcpy(op1, r, 25);
		bigDivide(r, op1, temp, 25, base58_shift_list);
		out[index--] = LOOKUP_BYTE(base58_char_list[op1[0]]);
	}
	out[35] = '\0';
	for (i = 0; i < 35; i++)
	{
		if (out[0] == '1')
		{
			for (j = 0; j < 35; j++)
			{
				out[j] = out[j + 1];
			}
		}
		else
		{
			break;
		}
	}
	for (i = 0; i < leading_zero_bytes; i++)
	{
		for (j = 34; j < 35; j--)
		{
			out[j + 1] = out[j];
		}
		out[0] = '1';
	}
}
void hashToAddr ( char * out , uint8_t * in , uint8_t address_version ) { uint8_t r [ 25 ] ; uint8_t op1 [ 26 ] ; uint8_t temp [ 26 ] ; uint8_t index ; uint8_t i ; uint8_t j ; uint8_t leading_zero_bytes ; uint32_t hs [ 8 ] ; sha256BeginTZ ( 4 ) ; r [ 24 ] = address_version ; sha256WriteTZ ( & ( address_version ) , ( uint32_t ) 1 , 4 ) ; for ( i = 0 ; i < 20 ; i ++ ) { r [ 23 - i ] = in [ i ] ; } sha256WriteTZ ( in , ( uint32_t ) 20 , 4 ) ; sha256FinishDoubleTZ ( hs , ( uint32_t ) 32 , 4 ) ; writeU32LittleEndian ( r , hs [ 0 ] ) ; leading_zero_bytes = 0 ; for ( i = 24 ; i < 25 ; i -- ) { if ( r [ i ] == 0 ) { leading_zero_bytes ++ ; } else { break ; } } index = 34 ; for ( i = 0 ; i < 35 ; i ++ ) { memcpy ( op1 , r , 25 ) ; bigDivide ( r , op1 , temp , 25 , base58_shift_list ) ; out [ index -- ] = LOOKUP_BYTE ( base58_char_list [ op1 [ 0 ] ] ) ; } out [ 35 ] = ' \0 ' ; for ( i = 0 ; i < 35 ; i ++ ) { if ( out [ 0 ] == ' ' ) { for ( j = 0 ; j < 35 ; j ++ ) { out [ j ] = out [ j + 1 ] ; } } else { break ; } } for ( i = 0 ; i < leading_zero_bytes ; i ++ ) { for ( j = 34 ; j < 35 ; j -- ) { out [ j + 1 ] = out [ j ] ; } out [ 0 ] = ' ' ; } }
Convert 160 bit hash to a human-readable base 58 Bitcoin address such
 as "1Dinox3mFw8yykpAZXFGEKeH4VX1Mzbcxe".
Convert 160 bit hash to a human - readable base 58 Bitcoin address such as " 1Dinox3mFw8yykpAZXFGEKeH4VX1Mzbcxe " .
//HashState hs; /* Prepend address version and append checksum. */ //sha256Begin(&hs); //sha256WriteByte(&hs, address_version); //sha256WriteByte(&hs, in[i]); //sha256FinishDouble(&hs); //writeU32LittleEndian(r, hs.h[0]); /* Count number of leading zero bytes. */ /* Convert to base 58. */ /* Remove leading zeroes. */ /*
	 * Insert leading zeroes equal in number to the number of input leading
	 * zero bytes.
	 */