Base58Check encoding: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 18:
{{trans|Python}}
 
<langsyntaxhighlight lang=11l>V ALPHABET = ‘123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz’
 
F convertToBase58(=num)
Line 35:
L(num) hash_arr
b = convertToBase58(BigInt(num, radix' 16))
print(‘0x#<54 -> #.’.format(num, b))</langsyntaxhighlight>
 
{{out}}
Line 53:
=={{header|C sharp|C#}}==
{{trans|Java}}
<langsyntaxhighlight lang=csharp>using System;
using System.Collections.Generic;
using System.Numerics;
Line 123:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>25420294593250030202636073700053352635053786165627414518 -> 6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
Line 137:
 
=={{header|D}}==
<langsyntaxhighlight lang=D>import std.bigint;
import std.stdio;
 
Line 185:
return sb.data.idup;
}
}</langsyntaxhighlight>
{{out}}
<pre>25420294593250030202636073700053352635053786165627414518 -> 6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
Line 200:
=={{header|FreeBASIC}}==
{{libheader|GMP}}
<langsyntaxhighlight lang=freebasic>' version 14-08-2017
' compile with: fbc -s console
' uses GMP
Line 259:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>OkobppXBkab(58) --> RosettaCode
Line 276:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang=go>package main
 
import (
Line 338:
fmt.Printf("%-56s -> %s\n", hash, b58)
}
}</langsyntaxhighlight>
 
{{out}}
Line 356:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang=groovy>class Base58CheckEncoding {
private static final String ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
private static final BigInteger BIG0 = BigInteger.ZERO
Line 404:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>25420294593250030202636073700053352635053786165627414518 -> 6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
Line 418:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang=haskell>import Numeric (showIntAtBase)
 
chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
Line 436:
0x572e4794,
0xecac89cad93923c02321,
0x10c8511e]</langsyntaxhighlight>
{{out}}
<pre>
Line 453:
and for bulk encoding, Array access would be one of various slightly faster alternatives to recursive subscripting of linked lists:
{{Trans|Python}}
<langsyntaxhighlight lang=Haskell>import Data.Array (Array, listArray, (!))
import Numeric (showHex, showIntAtBase)
 
Line 524:
<*> ((" -> " <>) . fxShow . f)
)
xs</langsyntaxhighlight>
{{Out}}
<pre>Base 58 encoding:
Line 542:
{{trans|Kotlin}}
{{works with|Java|9}}
<langsyntaxhighlight lang=Java>import java.math.BigInteger;
import java.util.List;
 
Line 593:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>25420294593250030202636073700053352635053786165627414518 -> 6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
Line 609:
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang=julia>const alpha = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
 
function encodebase58(hsh::AbstractString, base::Integer=16)
Line 628:
"0x10c8511e"]
println("# $s\n -> ", encodebase58(s))
end</langsyntaxhighlight>
 
{{out}}
Line 653:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang=scala>// version 1.1.51
 
import java.math.BigInteger
Line 692:
println("${hash.padEnd(56)} -> $b58")
}
}</langsyntaxhighlight>
 
{{out}}
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
<langsyntaxhighlight lang=Nim>import sequtils, strutils
import bignum
 
Line 766:
 
for h in Hashes:
echo h.alignLeft(MaxLength), " → ", h.toBase58</langsyntaxhighlight>
 
{{out}}
Line 781:
 
=={{header|Perl}}==
<langsyntaxhighlight lang=perl>use Math::BigInt;
 
sub encode_base58 {
Line 813:
0xecac89cad93923c02321
0x10c8511e
);</langsyntaxhighlight>
{{out}}
<pre>25420294593250030202636073700053352635053786165627414518 -> 6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
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.
<!--<langsyntaxhighlight lang=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 863:
<span style="color: #0000FF;">?</span><span style="color: #000000;">base58</span><span style="color: #0000FF;">(</span><span style="color: #008000;">x"ecac89cad93923c02321"</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">base58</span><span style="color: #0000FF;">(</span><span style="color: #008000;">x"10c8511e"</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 879:
 
=={{header|Picat}}==
<langsyntaxhighlight lang=Picat>main =>
Tests = [[25420294593250030202636073700053352635053786165627414518,"6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM"],
["0x61","2g"],
Line 904:
X := X // 58,
Out := Out ++ [CodeS[Rem+1]]
end.</langsyntaxhighlight>
 
{{out}}
Line 920:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight lang=PicoLisp>(setq *B58Alpha
(chop "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz") )
(de b58 (S)
Line 934:
(println (b58 "25420294593250030202636073700053352635053786165627414518"))
(println (b58 "0x626262"))
(println (b58 "0x636363"))</langsyntaxhighlight>
{{out}}
<pre>
Line 946:
===Works with Python 3.9
{{trans|C#}}
<langsyntaxhighlight lang=python>ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
 
def convertToBase58(num):
Line 963:
for num in hash_arr:
b = convertToBase58(num)
print("0x%-54x -> %s" % (num, b))</langsyntaxhighlight>
{{out}}
<pre>25420294593250030202636073700053352635053786165627414518 -> 6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
Line 978:
===Composition of pure functions===
{{Works with|Python|3.7}}
<langsyntaxhighlight lang=python>'''Base 58 check encoding'''
 
from functools import reduce
Line 1,138:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Base 58 check encoding:
Line 1,155:
=={{header|Quackery}}==
 
<langsyntaxhighlight lang=Quackery> [ table ] is base58char ( n --> n )
$ "123456789ABCDEFGHJKLMNPQRSTUV"
Line 1,181:
 
witheach [ base58$ echo$ cr ]
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,200:
=={{header|Racket}}==
(Examples from some other task)
<langsyntaxhighlight lang=racket>#lang racket
 
(define ((base-n-alphabet-encode alphabet) hash-string (in-base 16))
Line 1,226:
"0x572e4794"
"0xecac89cad93923c02321"
"0x10c8511e")))</langsyntaxhighlight>
{{out}}
<pre>"6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM"
Line 1,233:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang=raku perl6lines>sub encode_Base58 ( Int $x ) {
constant @codes = <
1 2 3 4 5 6 7 8 9
Line 1,259:
is encode_Base58(.key), .value, "{.key} encodes to {.value}";
}
</syntaxhighlight>
</lang>
 
=={{header|REXX}}==
Line 1,267:
I get the result expected there.
Apart for the leading 1 the program works also for the inputs shown above.
<langsyntaxhighlight lang=rexx>/* REXX */
s="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
Numeric Digits 100
Line 1,279:
End
o=o||substr(s,1,1)
Say reverse(o)</langsyntaxhighlight>
{{out}}
<pre>16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM</pre>
===version 2===
does what the others do
<langsyntaxhighlight lang=rexx>/* REXX */
s="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
Numeric Digits 1000
Line 1,319:
Say 'found :' o
End
Return</langsyntaxhighlight>
{{out}}
<pre>expected: check_error_handlimng
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'').
<langsyntaxhighlight 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,351:
return right(oz, 60) "───►" left('', L1, 0)$ /*for showing arg and the residual. */
 
</syntaxhighlight>
</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,367:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang=ruby>ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
nums = [25420294593250030202636073700053352635053786165627414518,
0x61,
Line 1,380:
 
puts nums.map{|n| n.digits(58).reverse.map{|i| ALPHABET[i]}.join}
</syntaxhighlight>
</lang>
{{out}}
<pre>6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
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)].
<langsyntaxhighlight lang=Scala>import java.math.BigInteger
object Base58 extends App {
Line 1,448:
}
 
}</langsyntaxhighlight>
 
=={{header|Seed7}}==
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.
<langsyntaxhighlight lang=seed7>$ include "seed7_05.s7i";
include "encoding.s7i";
 
Line 1,471:
writeln("16#" <& num radix 16 rpad 53 <& " -> " <& b);
end for;
end func;</langsyntaxhighlight>
 
{{out}}
Line 1,487:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang=ruby>func encode_base58(n) {
static chars = %w(
1 2 3 4 5 6 7 8 9
Line 1,507:
printf("%56s -> %s\n", num, encode_base58(num))
assert_eq(encode_base58(num), enc)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,524:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang=vbnet>Imports System.Numerics
Imports System.Text
 
Line 1,581:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>25420294593250030202636073700053352635053786165627414518 -> 6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
Line 1,598:
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang=ecmascript>import "/big" for BigInt
import "/fmt" for Fmt
 
Line 1,633:
var b58 = convertToBase58.call(hash, 16)
Fmt.print("$-56s -> $s", hash, b58)
}</langsyntaxhighlight>
 
{{out}}
Line 1,651:
=={{header|zkl}}==
Uses libGMP
<langsyntaxhighlight lang=zkl>var [const] BN=Import.lib("zklBigNum"), // GNU Multiple Precision Arithmetic Library
src="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv",
dst="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
Line 1,663:
0x516b6fcd0f, "bf4f89001e670274dd", 0x572e4794,
"ecac89cad93923c02321", 0x10c8511e);
ns.pump(Console.println,'wrap(n){ BN(n,16).toString(58).translate(src,dst) });</langsyntaxhighlight>
{{out}}
<pre>
10,327

edits