Base58Check encoding: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 1: Line 1:
{{draft task}}
[[Category:Encodings]]
[[Category:Encodings]]
[[Category:Checksums]]
[[Category:Checksums]]
{{draft task}}


The popular encoding of small and medium-sized [[:Category:Checksums|checksums]] is [[wp:base16|base16]], that is more compact than usual base10 and is human readable... For checksums resulting in ''hash digests'' bigger than ~100 bits, the base16 is too long: [[wp:base58|base58]] is shorter and (when using good alphabet) preserves secure human readability. The most popular alphabet of base58 is the variant used in bitcoin address (see [[Bitcoin/address validation]]), so it is the "default base58 alphabet".
The popular encoding of small and medium-sized [[:Category:Checksums|checksums]] is [[wp:base16|base16]], that is more compact than usual base10 and is human readable... For checksums resulting in ''hash digests'' bigger than ~100 bits, the base16 is too long: [[wp:base58|base58]] is shorter and (when using good alphabet) preserves secure human readability. The most popular alphabet of base58 is the variant used in bitcoin address (see [[Bitcoin/address validation]]), so it is the "default base58 alphabet".
Line 18: Line 18:
{{trans|Python}}
{{trans|Python}}


<syntaxhighlight lang=11l>V ALPHABET = ‘123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz’
<syntaxhighlight lang="11l">V ALPHABET = ‘123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz’


F convertToBase58(=num)
F convertToBase58(=num)
Line 53: Line 53:
=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
{{trans|Java}}
{{trans|Java}}
<syntaxhighlight lang=csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Numerics;
using System.Numerics;
Line 137: Line 137:


=={{header|D}}==
=={{header|D}}==
<syntaxhighlight lang=D>import std.bigint;
<syntaxhighlight lang="d">import std.bigint;
import std.stdio;
import std.stdio;


Line 200: Line 200:
=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
{{libheader|GMP}}
{{libheader|GMP}}
<syntaxhighlight lang=freebasic>' version 14-08-2017
<syntaxhighlight lang="freebasic">' version 14-08-2017
' compile with: fbc -s console
' compile with: fbc -s console
' uses GMP
' uses GMP
Line 276: Line 276:
=={{header|Go}}==
=={{header|Go}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<syntaxhighlight lang=go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 356: Line 356:
=={{header|Groovy}}==
=={{header|Groovy}}==
{{trans|Java}}
{{trans|Java}}
<syntaxhighlight lang=groovy>class Base58CheckEncoding {
<syntaxhighlight lang="groovy">class Base58CheckEncoding {
private static final String ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
private static final String ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
private static final BigInteger BIG0 = BigInteger.ZERO
private static final BigInteger BIG0 = BigInteger.ZERO
Line 418: Line 418:


=={{header|Haskell}}==
=={{header|Haskell}}==
<syntaxhighlight lang=haskell>import Numeric (showIntAtBase)
<syntaxhighlight lang="haskell">import Numeric (showIntAtBase)


chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
Line 453: Line 453:
and for bulk encoding, Array access would be one of various slightly faster alternatives to recursive subscripting of linked lists:
and for bulk encoding, Array access would be one of various slightly faster alternatives to recursive subscripting of linked lists:
{{Trans|Python}}
{{Trans|Python}}
<syntaxhighlight lang=Haskell>import Data.Array (Array, listArray, (!))
<syntaxhighlight lang="haskell">import Data.Array (Array, listArray, (!))
import Numeric (showHex, showIntAtBase)
import Numeric (showHex, showIntAtBase)


Line 542: Line 542:
{{trans|Kotlin}}
{{trans|Kotlin}}
{{works with|Java|9}}
{{works with|Java|9}}
<syntaxhighlight lang=Java>import java.math.BigInteger;
<syntaxhighlight lang="java">import java.math.BigInteger;
import java.util.List;
import java.util.List;


Line 609: Line 609:
{{works with|Julia|0.6}}
{{works with|Julia|0.6}}


<syntaxhighlight lang=julia>const alpha = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
<syntaxhighlight lang="julia">const alpha = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"


function encodebase58(hsh::AbstractString, base::Integer=16)
function encodebase58(hsh::AbstractString, base::Integer=16)
Line 653: Line 653:


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<syntaxhighlight lang=scala>// version 1.1.51
<syntaxhighlight lang="scala">// version 1.1.51


import java.math.BigInteger
import java.math.BigInteger
Line 711: Line 711:
{{libheader|bignum}}
{{libheader|bignum}}
This version takes in account the leading zeroes in hexadecimal representation. It accepts also arrays or sequences of bytes as input, taking in account the leading zero bytes as described in https://en.bitcoin.it/wiki/Base58Check_encoding#Base58_symbol_chart
This version takes in account the leading zeroes in hexadecimal representation. It accepts also arrays or sequences of bytes as input, taking in account the leading zero bytes as described in https://en.bitcoin.it/wiki/Base58Check_encoding#Base58_symbol_chart
<syntaxhighlight lang=Nim>import sequtils, strutils
<syntaxhighlight lang="nim">import sequtils, strutils
import bignum
import bignum


Line 781: Line 781:


=={{header|Perl}}==
=={{header|Perl}}==
<syntaxhighlight lang=perl>use Math::BigInt;
<syntaxhighlight lang="perl">use Math::BigInt;


sub encode_base58 {
sub encode_base58 {
Line 829: Line 829:
Slight variation from [[Bitcoin/public_point_to_address#Phix]] in that it accepts any length string (which can be binary or text).<br>
Slight variation from [[Bitcoin/public_point_to_address#Phix]] in that it accepts any length string (which can be binary or text).<br>
Includes leading zeroes, if you don't want that just comment out the three lines defining/using the integer lz.
Includes leading zeroes, if you don't want that just comment out the three lines defining/using the integer lz.
<!--<syntaxhighlight lang=Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">b58</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">b58</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"</span>
Line 879: Line 879:


=={{header|Picat}}==
=={{header|Picat}}==
<syntaxhighlight lang=Picat>main =>
<syntaxhighlight lang="picat">main =>
Tests = [[25420294593250030202636073700053352635053786165627414518,"6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM"],
Tests = [[25420294593250030202636073700053352635053786165627414518,"6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM"],
["0x61","2g"],
["0x61","2g"],
Line 920: Line 920:


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<syntaxhighlight lang=PicoLisp>(setq *B58Alpha
<syntaxhighlight lang="picolisp">(setq *B58Alpha
(chop "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz") )
(chop "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz") )
(de b58 (S)
(de b58 (S)
Line 946: Line 946:
===Works with Python 3.9
===Works with Python 3.9
{{trans|C#}}
{{trans|C#}}
<syntaxhighlight lang=python>ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
<syntaxhighlight lang="python">ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"


def convertToBase58(num):
def convertToBase58(num):
Line 978: Line 978:
===Composition of pure functions===
===Composition of pure functions===
{{Works with|Python|3.7}}
{{Works with|Python|3.7}}
<syntaxhighlight lang=python>'''Base 58 check encoding'''
<syntaxhighlight lang="python">'''Base 58 check encoding'''


from functools import reduce
from functools import reduce
Line 1,155: Line 1,155:
=={{header|Quackery}}==
=={{header|Quackery}}==


<syntaxhighlight lang=Quackery> [ table ] is base58char ( n --> n )
<syntaxhighlight lang="quackery"> [ table ] is base58char ( n --> n )
$ "123456789ABCDEFGHJKLMNPQRSTUV"
$ "123456789ABCDEFGHJKLMNPQRSTUV"
Line 1,200: Line 1,200:
=={{header|Racket}}==
=={{header|Racket}}==
(Examples from some other task)
(Examples from some other task)
<syntaxhighlight lang=racket>#lang racket
<syntaxhighlight lang="racket">#lang racket


(define ((base-n-alphabet-encode alphabet) hash-string (in-base 16))
(define ((base-n-alphabet-encode alphabet) hash-string (in-base 16))
Line 1,233: Line 1,233:
=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
<syntaxhighlight lang=raku lines>sub encode_Base58 ( Int $x ) {
<syntaxhighlight lang="raku" line>sub encode_Base58 ( Int $x ) {
constant @codes = <
constant @codes = <
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
Line 1,267: Line 1,267:
I get the result expected there.
I get the result expected there.
Apart for the leading 1 the program works also for the inputs shown above.
Apart for the leading 1 the program works also for the inputs shown above.
<syntaxhighlight lang=rexx>/* REXX */
<syntaxhighlight lang="rexx">/* REXX */
s="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
s="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
Numeric Digits 100
Numeric Digits 100
Line 1,284: Line 1,284:
===version 2===
===version 2===
does what the others do
does what the others do
<syntaxhighlight lang=rexx>/* REXX */
<syntaxhighlight lang="rexx">/* REXX */
s="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
s="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
Numeric Digits 1000
Numeric Digits 1000
Line 1,331: Line 1,331:


The algorithm used doesn't need to &nbsp; ''reverse'' &nbsp; the residual string &nbsp; (it uses &nbsp; ''prepend'' &nbsp; instead of &nbsp; ''append'').
The algorithm used doesn't need to &nbsp; ''reverse'' &nbsp; the residual string &nbsp; (it uses &nbsp; ''prepend'' &nbsp; instead of &nbsp; ''append'').
<syntaxhighlight lang=rexx>/*REXX pgm encodes a checksum (hash digest) into Base58 (the standard Bitcoin alphabet).*/
<syntaxhighlight lang="rexx">/*REXX pgm encodes a checksum (hash digest) into Base58 (the standard Bitcoin alphabet).*/
/* 0─────────────────I─────O────────────────────l──────────────── ◄───omit.*/
/* 0─────────────────I─────O────────────────────l──────────────── ◄───omit.*/
@= space(" 123456789ABCDEFGH JKLMN PQRSTUVWXYZabcdefghi jkmnopqrstuvwxyz", 0)
@= space(" 123456789ABCDEFGH JKLMN PQRSTUVWXYZabcdefghi jkmnopqrstuvwxyz", 0)
Line 1,367: Line 1,367:


=={{header|Ruby}}==
=={{header|Ruby}}==
<syntaxhighlight lang=ruby>ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
<syntaxhighlight lang="ruby">ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
nums = [25420294593250030202636073700053352635053786165627414518,
nums = [25420294593250030202636073700053352635053786165627414518,
0x61,
0x61,
Line 1,396: Line 1,396:
=={{header|Scala}}==
=={{header|Scala}}==
{{Out}}Best seen in running your browser either by [https://scalafiddle.io/sf/GMcrlBB/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org Scastie (remote JVM)].
{{Out}}Best seen in running your browser either by [https://scalafiddle.io/sf/GMcrlBB/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org Scastie (remote JVM)].
<syntaxhighlight lang=Scala>import java.math.BigInteger
<syntaxhighlight lang="scala">import java.math.BigInteger
object Base58 extends App {
object Base58 extends App {
Line 1,454: Line 1,454:
the function [http://seed7.sourceforge.net/libraries/encoding.htm#toBase(in_bigInteger,in_string) toBase],
the function [http://seed7.sourceforge.net/libraries/encoding.htm#toBase(in_bigInteger,in_string) toBase],
which encodes a number with a positional numeric system. No external library is needed.
which encodes a number with a positional numeric system. No external library is needed.
<syntaxhighlight lang=seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "encoding.s7i";
include "encoding.s7i";


Line 1,487: Line 1,487:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Raku}}
{{trans|Raku}}
<syntaxhighlight lang=ruby>func encode_base58(n) {
<syntaxhighlight lang="ruby">func encode_base58(n) {
static chars = %w(
static chars = %w(
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
Line 1,524: Line 1,524:
=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
{{trans|C#}}
{{trans|C#}}
<syntaxhighlight lang=vbnet>Imports System.Numerics
<syntaxhighlight lang="vbnet">Imports System.Numerics
Imports System.Text
Imports System.Text


Line 1,598: Line 1,598:
{{libheader|Wren-big}}
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang=ecmascript>import "/big" for BigInt
<syntaxhighlight lang="ecmascript">import "/big" for BigInt
import "/fmt" for Fmt
import "/fmt" for Fmt


Line 1,651: Line 1,651:
=={{header|zkl}}==
=={{header|zkl}}==
Uses libGMP
Uses libGMP
<syntaxhighlight lang=zkl>var [const] BN=Import.lib("zklBigNum"), // GNU Multiple Precision Arithmetic Library
<syntaxhighlight lang="zkl">var [const] BN=Import.lib("zklBigNum"), // GNU Multiple Precision Arithmetic Library
src="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv",
src="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv",
dst="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
dst="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";