Base58Check encoding: Difference between revisions

m (Automated syntax highlighting fixup (second round - minor fixes))
Line 605:
0xecac89cad93923c02321 -> EJDM8drfXA6uyA
0x10c8511e -> Rt5zm</pre>
 
=={{header|jq}}==
'''Works with gojq, the Go implementation of jq'''
 
WARNING: The program will also run using the C implementation of jq but the results for the very
large values will be inaccurate.
 
'''Generic utility functions'''
<syntaxhighlight lang=jq>
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
# Input: a string in base $b (2 to 35 inclusive)
# Output: the decimal value
def frombase($b):
def decimalValue:
if 48 <= . and . <= 57 then . - 48
elif 65 <= . and . <= 90 then . - 55 # (10+.-65)
elif 97 <= . and . <= 122 then . - 87 # (10+.-97)
else "decimalValue" | error
end;
reduce (explode|reverse[]|decimalValue) as $x ({p:1};
.value += (.p * $x)
| .p *= $b)
| .value ;
</syntaxhighlight>
'''Base58Check'''
<syntaxhighlight lang=jq>
# The base58check alphabet, i.e. 0 => "1", etc
def alphabet: "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
 
# input: a string in the specified $base
def convertToBase58($base):
. as $hash
| {x: (if $base == 16 and ($hash|startswith("0x"))
then $hash[2:]|frombase(16)
else $hash|frombase($base)
end),
sb: [] }
| until (.x <= 0;
(.x % 58) as $r
| .sb += [alphabet[$r:$r+1]]
| .x |= (. - $r) / 58 )
| .sb | reverse | join("");
 
def hashes: [
"0x61",
"0x626262",
"0x636363",
"0x73696d706c792061206c6f6e6720737472696e67",
"0x516b6fcd0f",
"0xbf4f89001e670274dd",
"0x572e4794",
"0xecac89cad93923c02321",
"0x10c8511e"
];
 
def task:
def s: "25420294593250030202636073700053352635053786165627414518";
 
(s | "\(lpad(58))-> \(convertToBase58(10))" ),
(hashes[]
| [lpad(58), convertToBase58(16)] | join("-> ") ) ;
 
task
</syntaxhighlight>
{{output}}
<pre>
25420294593250030202636073700053352635053786165627414518-> 6UwLL9RisZVooooooooooooooooooooo
0x61-> 2g
0x626262-> a3gV
0x636363-> aPEr
0x73696d706c792061206c6f6e6720737472696e67-> 2cFupjhnEuPooooooooooooooooo
0x516b6fcd0f-> ABnLTmg
0xbf4f89001e670274dd-> 3SEo3LWLoMXoo
0x572e4794-> 3EFU7m
0xecac89cad93923c02321-> EJDM8drfX5mooo
0x10c8511e-> Rt5zm
</pre>
 
=={{header|Julia}}==
2,442

edits