Base58Check encoding: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 1:
{{draft task}}
[[Category:Encodings]]
[[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".
Line 18:
{{trans|Python}}
 
<syntaxhighlight lang="11l">V ALPHABET = ‘123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz’
 
F convertToBase58(=num)
Line 53:
=={{header|C sharp|C#}}==
{{trans|Java}}
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Numerics;
Line 137:
 
=={{header|D}}==
<syntaxhighlight lang=D"d">import std.bigint;
import std.stdio;
 
Line 200:
=={{header|FreeBASIC}}==
{{libheader|GMP}}
<syntaxhighlight lang="freebasic">' version 14-08-2017
' compile with: fbc -s console
' uses GMP
Line 276:
=={{header|Go}}==
{{trans|Kotlin}}
<syntaxhighlight lang="go">package main
 
import (
Line 356:
=={{header|Groovy}}==
{{trans|Java}}
<syntaxhighlight lang="groovy">class Base58CheckEncoding {
private static final String ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
private static final BigInteger BIG0 = BigInteger.ZERO
Line 418:
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">import Numeric (showIntAtBase)
 
chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
Line 453:
and for bulk encoding, Array access would be one of various slightly faster alternatives to recursive subscripting of linked lists:
{{Trans|Python}}
<syntaxhighlight lang=Haskell"haskell">import Data.Array (Array, listArray, (!))
import Numeric (showHex, showIntAtBase)
 
Line 542:
{{trans|Kotlin}}
{{works with|Java|9}}
<syntaxhighlight lang=Java"java">import java.math.BigInteger;
import java.util.List;
 
Line 609:
{{works with|Julia|0.6}}
 
<syntaxhighlight lang="julia">const alpha = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
 
function encodebase58(hsh::AbstractString, base::Integer=16)
Line 653:
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.1.51
 
import java.math.BigInteger
Line 711:
{{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
<syntaxhighlight lang=Nim"nim">import sequtils, strutils
import bignum
 
Line 781:
 
=={{header|Perl}}==
<syntaxhighlight lang="perl">use Math::BigInt;
 
sub encode_base58 {
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>
Includes leading zeroes, if you don't want that just comment out the three lines defining/using the integer lz.
<!--<syntaxhighlight lang=Phix"phix">(phixonline)-->
<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>
Line 879:
 
=={{header|Picat}}==
<syntaxhighlight lang=Picat"picat">main =>
Tests = [[25420294593250030202636073700053352635053786165627414518,"6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM"],
["0x61","2g"],
Line 920:
 
=={{header|PicoLisp}}==
<syntaxhighlight lang=PicoLisp"picolisp">(setq *B58Alpha
(chop "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz") )
(de b58 (S)
Line 946:
===Works with Python 3.9
{{trans|C#}}
<syntaxhighlight lang="python">ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
 
def convertToBase58(num):
Line 978:
===Composition of pure functions===
{{Works with|Python|3.7}}
<syntaxhighlight lang="python">'''Base 58 check encoding'''
 
from functools import reduce
Line 1,155:
=={{header|Quackery}}==
 
<syntaxhighlight lang=Quackery"quackery"> [ table ] is base58char ( n --> n )
$ "123456789ABCDEFGHJKLMNPQRSTUV"
Line 1,200:
=={{header|Racket}}==
(Examples from some other task)
<syntaxhighlight lang="racket">#lang racket
 
(define ((base-n-alphabet-encode alphabet) hash-string (in-base 16))
Line 1,233:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" linesline>sub encode_Base58 ( Int $x ) {
constant @codes = <
1 2 3 4 5 6 7 8 9
Line 1,267:
I get the result expected there.
Apart for the leading 1 the program works also for the inputs shown above.
<syntaxhighlight lang="rexx">/* REXX */
s="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
Numeric Digits 100
Line 1,284:
===version 2===
does what the others do
<syntaxhighlight lang="rexx">/* REXX */
s="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
Numeric Digits 1000
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'').
<syntaxhighlight lang="rexx">/*REXX pgm encodes a checksum (hash digest) into Base58 (the standard Bitcoin alphabet).*/
/* 0─────────────────I─────O────────────────────l──────────────── ◄───omit.*/
@= space(" 123456789ABCDEFGH JKLMN PQRSTUVWXYZabcdefghi jkmnopqrstuvwxyz", 0)
Line 1,367:
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
nums = [25420294593250030202636073700053352635053786165627414518,
0x61,
Line 1,396:
=={{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)].
<syntaxhighlight lang=Scala"scala">import java.math.BigInteger
object Base58 extends App {
Line 1,454:
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.
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "encoding.s7i";
 
Line 1,487:
=={{header|Sidef}}==
{{trans|Raku}}
<syntaxhighlight lang="ruby">func encode_base58(n) {
static chars = %w(
1 2 3 4 5 6 7 8 9
Line 1,524:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<syntaxhighlight lang="vbnet">Imports System.Numerics
Imports System.Text
 
Line 1,598:
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascript">import "/big" for BigInt
import "/fmt" for Fmt
 
Line 1,651:
=={{header|zkl}}==
Uses libGMP
<syntaxhighlight lang="zkl">var [const] BN=Import.lib("zklBigNum"), // GNU Multiple Precision Arithmetic Library
src="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv",
dst="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
10,333

edits