Bitcoin/address validation: 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:
[[Category:Checksums]]
{{alertbox|#ffff70|'''<big>Warning:</big>''' Many of these snippets are [[{{TALKPAGENAME}}#C-based_code_.28and_possibly_others.29_improperly_validates|incomplete]]. It is recommended that you use an established [https://en.bitcoin.it/wiki/Software#Libraries library] for any projects that are likely to see external use}}
{{alertbox|#ffff70|'''<big>Warning:</big>''' Many of these snippets are [[{{TALKPAGENAME}}#C-based_code_.28and_possibly_others.29_improperly_validates|incomplete]]. It is recommended that you use an established [https://en.bitcoin.it/wiki/Software#Libraries library] for any projects that are likely to see external use}}
{{task}}
{{task}}
[[Category:Checksums]]
{{omit from|Brlcad}}
{{omit from|GUISS}}




Line 38: Line 36:
<br>You can change a few characters in this string and check that it'll fail the test.
<br>You can change a few characters in this string and check that it'll fail the test.
<br><br>
<br><br>

=={{header|Ada}}==
=={{header|Ada}}==
<syntaxhighlight lang=ada>with Ada.Exceptions, Interfaces;
<syntaxhighlight lang="ada">with Ada.Exceptions, Interfaces;
with Ada.Streams;
with Ada.Streams;
use Ada.Exceptions, Interfaces;
use Ada.Exceptions, Interfaces;
Line 177: Line 174:
1A Na15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i validity: *** Error: Invalid BT address.
1A Na15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i validity: *** Error: Invalid BT address.
</pre>
</pre>

=={{header|C}}==
=={{header|C}}==
<syntaxhighlight lang=c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
#include <string.h>
#include <openssl/sha.h>
#include <openssl/sha.h>
Line 248: Line 244:
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I: bad char
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I: bad char
</pre>
</pre>

=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
This requires [https://www.nuget.org/packages/NUnit/ NUnit package] to compile.
This requires [https://www.nuget.org/packages/NUnit/ NUnit package] to compile.
<syntaxhighlight lang=csharp>
<syntaxhighlight lang="csharp">
using System;
using System;
using System.Linq;
using System.Linq;
Line 322: Line 317:
}
}
</syntaxhighlight>
</syntaxhighlight>

=={{header|D}}==
=={{header|D}}==
This requires the D module from the SHA-256 Task.
This requires the D module from the SHA-256 Task.
{{trans|Go}}
{{trans|Go}}
<syntaxhighlight lang=d>import std.stdio, std.algorithm, std.array, std.string, sha_256_2;
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.array, std.string, sha_256_2;


struct A25 {
struct A25 {
Line 417: Line 411:
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62iz": not Bitcoin version 0.
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62iz": not Bitcoin version 0.
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62izz": too long Bitcoin address.</pre>
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62izz": too long Bitcoin address.</pre>

=={{header|dart}}==
=={{header|dart}}==
This requires [https://pub.dev/packages/crypto Crypto package] to compile.
This requires [https://pub.dev/packages/crypto Crypto package] to compile.
{{trans|Java}}
{{trans|Java}}
<syntaxhighlight lang=dart>import 'package:crypto/crypto.dart';
<syntaxhighlight lang="dart">import 'package:crypto/crypto.dart';


class Bitcoin {
class Bitcoin {
Line 524: Line 517:
"1BNGaR29FmfAqidXmD9HLws" false
"1BNGaR29FmfAqidXmD9HLws" false
</pre>
</pre>

=={{header|Delphi}}==
=={{header|Delphi}}==
This requires [http://www.cityinthesky.co.uk/opensource/DCPcrypt/ DCPcrypt library] to compile.
This requires [http://www.cityinthesky.co.uk/opensource/DCPcrypt/ DCPcrypt library] to compile.
<syntaxhighlight lang=delphi>
<syntaxhighlight lang="delphi">
uses
uses
DCPsha256;
DCPsha256;
Line 593: Line 585:
end;
end;
</syntaxhighlight>
</syntaxhighlight>

=={{header|Erlang}}==
=={{header|Erlang}}==
Using base58 module from http://github.com/titan098/erl-base58.git.
Using base58 module from http://github.com/titan098/erl-base58.git.


<syntaxhighlight lang=Erlang>
<syntaxhighlight lang="erlang">
-module( bitcoin_address ).
-module( bitcoin_address ).


Line 626: Line 617:
in call from bitcoin_address:task/0 (src/bitcoin_address.erl, line 9)
in call from bitcoin_address:task/0 (src/bitcoin_address.erl, line 9)
</pre>
</pre>

=={{header|Factor}}==
=={{header|Factor}}==
<syntaxhighlight lang=factor>USING: byte-arrays checksums checksums.sha io.binary kernel math
<syntaxhighlight lang="factor">USING: byte-arrays checksums checksums.sha io.binary kernel math
math.parser sequences ;
math.parser sequences ;
IN: rosetta-code.bitcoin.validation
IN: rosetta-code.bitcoin.validation
Line 652: Line 642:
"1ANNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i" btc-valid? . ! f, data changed, original checksum.
"1ANNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i" btc-valid? . ! f, data changed, original checksum.
</pre>
</pre>

=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<syntaxhighlight lang=freebasic>' version 05-04-2017
<syntaxhighlight lang="freebasic">' version 05-04-2017
' compile with: fbc -s console
' compile with: fbc -s console


Line 866: Line 855:
Bitcoin address: 0AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i first character is not 1 or 3
Bitcoin address: 0AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i first character is not 1 or 3
Bitcoin address: 1AGNa15ZQXAZUgFlqJ2i7Z2DPU2J6hW62i bitcoin address contains illegal character</pre>
Bitcoin address: 1AGNa15ZQXAZUgFlqJ2i7Z2DPU2J6hW62i bitcoin address contains illegal character</pre>

=={{header|Go}}==
=={{header|Go}}==
{{trans|C}}
{{trans|C}}
<syntaxhighlight lang=go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 1,002: Line 990:
1
1
</pre>
</pre>

=={{header|Haskell}}==
=={{header|Haskell}}==
<syntaxhighlight lang=haskell>import Control.Monad (when)
<syntaxhighlight lang="haskell">import Control.Monad (when)
import Data.List (elemIndex)
import Data.List (elemIndex)
import Data.Monoid ((<>))
import Data.Monoid ((<>))
Line 1,071: Line 1,058:
"1ANa55215ZQXAZUgFiqJ2i7Z2DPU2J6hW62i" -> "Address length exceeds 25 bytes"
"1ANa55215ZQXAZUgFiqJ2i7Z2DPU2J6hW62i" -> "Address length exceeds 25 bytes"
"i55j" -> "Address length less than 4 bytes"</pre>
"i55j" -> "Address length less than 4 bytes"</pre>

=={{header|Java}}==
=={{header|Java}}==
<syntaxhighlight lang=java>import java.math.BigInteger;
<syntaxhighlight lang="java">import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchAlgorithmException;
Line 1,142: Line 1,128:
}
}
}</syntaxhighlight>
}</syntaxhighlight>

=={{header|Julia}}==
=={{header|Julia}}==
{{works with|Julia|0.6}}
{{works with|Julia|0.6}}
{{trans|Python}}
{{trans|Python}}
<syntaxhighlight lang=julia>using SHA
<syntaxhighlight lang="julia">using SHA


bytes(n::Integer, l::Int) = collect(UInt8, (n >> 8i) & 0xFF for i in l-1:-1:0)
bytes(n::Integer, l::Int) = collect(UInt8, (n >> 8i) & 0xFF for i in l-1:-1:0)
Line 1,212: Line 1,197:
Address: 1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62iz
Address: 1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62iz
Expected: false Checked: false</pre>
Expected: false Checked: false</pre>

=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Java}}
{{trans|Java}}
<syntaxhighlight lang=scala>import java.security.MessageDigest
<syntaxhighlight lang="scala">import java.security.MessageDigest


object Bitcoin {
object Bitcoin {
Line 1,292: Line 1,276:
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I -> invalid
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I -> invalid
</pre>
</pre>

=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica>chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; data =
<syntaxhighlight lang="mathematica">chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; data =
IntegerDigits[
IntegerDigits[
FromDigits[
FromDigits[
Line 1,310: Line 1,293:
<pre>True
<pre>True
False</pre>
False</pre>

=={{header|Nim}}==
=={{header|Nim}}==
Tests on first digit character and on address length have been added to detect wrong addresses such as "BZbvjr".
Tests on first digit character and on address length have been added to detect wrong addresses such as "BZbvjr".
Line 1,316: Line 1,298:
===Using “libssl”===
===Using “libssl”===


<syntaxhighlight lang=nim>import algorithm
<syntaxhighlight lang="nim">import algorithm


const SHA256Len = 32
const SHA256Len = 32
Line 1,414: Line 1,396:
===Using “nimcrypto”===
===Using “nimcrypto”===
{{libheader|nimcrypto}}
{{libheader|nimcrypto}}
<syntaxhighlight lang=Nim>import nimcrypto
<syntaxhighlight lang="nim">import nimcrypto
import strformat
import strformat


Line 1,507: Line 1,489:
Address “1111111111111111111114oLvT2” is valid.
Address “1111111111111111111114oLvT2” is valid.
Address “BZbvjr” is invalid (starting character is not 1 or 3).</pre>
Address “BZbvjr” is invalid (starting character is not 1 or 3).</pre>

=={{header|Oberon-2}}==
=={{header|Oberon-2}}==
{{works with|oo2c}}{{libheader|Crypto}}
{{works with|oo2c}}{{libheader|Crypto}}
<syntaxhighlight lang=oberon2>
<syntaxhighlight lang="oberon2">
MODULE BitcoinAddress;
MODULE BitcoinAddress;
IMPORT
IMPORT
Line 1,600: Line 1,581:
FALSE
FALSE
</pre>
</pre>

=={{header|Perl}}==
=={{header|Perl}}==
<syntaxhighlight lang=perl>my @b58 = qw{
<syntaxhighlight lang="perl">my @b58 = qw{
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
A B C D E F G H J K L M N P Q R S T U V W X Y Z
A B C D E F G H J K L M N P Q R S T U V W X Y Z
Line 1,634: Line 1,614:
substr sha256(sha256 pack 'C*', @byte[0..20]), 0, 4;
substr sha256(sha256 pack 'C*', @byte[0..20]), 0, 4;
}</syntaxhighlight>
}</syntaxhighlight>

=={{header|Phix}}==
=={{header|Phix}}==
<!--<syntaxhighlight lang=Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\bitcoin_address_validation.exw
-- demo\rosetta\bitcoin_address_validation.exw
Line 1,722: Line 1,701:
<!--</syntaxhighlight>-->
<!--</syntaxhighlight>-->
(No output other than "done" since all tests pass)
(No output other than "done" since all tests pass)

=={{header|PHP}}==
=={{header|PHP}}==
<syntaxhighlight lang=php>
<syntaxhighlight lang="php">
function validate($address){
function validate($address){
$decoded = decodeBase58($address);
$decoded = decodeBase58($address);
Line 1,791: Line 1,769:
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I: invalid character found
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I: invalid character found
</pre>
</pre>

=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<syntaxhighlight lang=PicoLisp>(load "sha256.l")
<syntaxhighlight lang="picolisp">(load "sha256.l")


(setq *Alphabet
(setq *Alphabet
Line 1,823: Line 1,800:
(valid "1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62iz")
(valid "1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62iz")
(valid "1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62izz") ) )</syntaxhighlight>
(valid "1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62izz") ) )</syntaxhighlight>

=={{header|PureBasic}}==
=={{header|PureBasic}}==
<syntaxhighlight lang=PureBasic>
<syntaxhighlight lang="purebasic">
; using PureBasic 5.50 (x64)
; using PureBasic 5.50 (x64)
EnableExplicit
EnableExplicit
Line 1,911: Line 1,887:
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I -> Invalid
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I -> Invalid
</pre>
</pre>

=={{header|Python}}==
=={{header|Python}}==
<syntaxhighlight lang=python>from hashlib import sha256
<syntaxhighlight lang="python">from hashlib import sha256


digits58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
digits58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
Line 1,940: Line 1,915:
:Yuuki-chan edit: Delete this help if it's not needed anymore
:Yuuki-chan edit: Delete this help if it's not needed anymore
:For those looking at examples here to try and work out what is required, the <code>n.to_bytes()</code> call is equivalent to this code which converts a (long) integer into individual bytes of a byte array in a particular order:
:For those looking at examples here to try and work out what is required, the <code>n.to_bytes()</code> call is equivalent to this code which converts a (long) integer into individual bytes of a byte array in a particular order:
:<syntaxhighlight lang=python>>>> n = 2491969579123783355964723219455906992268673266682165637887
:<syntaxhighlight lang="python">>>> n = 2491969579123783355964723219455906992268673266682165637887
>>> length = 25
>>> length = 25
>>> list( reversed(range(length)) )
>>> list( reversed(range(length)) )
Line 1,946: Line 1,921:
>>> assert n.to_bytes(length, 'big') == bytes( (n >> i*8) & 0xff for i in reversed(range(length)))
>>> assert n.to_bytes(length, 'big') == bytes( (n >> i*8) & 0xff for i in reversed(range(length)))
>>> </syntaxhighlight>
>>> </syntaxhighlight>

=={{header|Racket}}==
=={{header|Racket}}==
<syntaxhighlight lang=racket>
<syntaxhighlight lang="racket">
#lang racket/base
#lang racket/base


Line 1,994: Line 1,968:
(validate-bitcoin-address "1badbadbadbadbadbadbadbadbadbadbad") ; => #f
(validate-bitcoin-address "1badbadbadbadbadbadbadbadbadbadbad") ; => #f
</syntaxhighlight>
</syntaxhighlight>

=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
<syntaxhighlight lang=raku line>sub sha256(blob8 $b) returns blob8 {
<syntaxhighlight lang="raku" line>sub sha256(blob8 $b) returns blob8 {
given run <openssl dgst -sha256 -binary>, :in, :out, :bin {
given run <openssl dgst -sha256 -binary>, :in, :out, :bin {
.in.write: $b;
.in.write: $b;
Line 2,023: Line 1,996:
{{out}}
{{out}}
<pre>「1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i」</pre>
<pre>「1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i」</pre>

=={{header|Ruby}}==
=={{header|Ruby}}==
<syntaxhighlight lang=ruby>
<syntaxhighlight lang="ruby">
# Validate Bitcoin address
# Validate Bitcoin address
#
#
Line 2,054: Line 2,026:
I calculate that it is c046b2ff
I calculate that it is c046b2ff
</pre>
</pre>

=={{header|Rust}}==
=={{header|Rust}}==
This requires the [https://crates.io/crates/rust-crypto rust-crypto] crate for sha256.
This requires the [https://crates.io/crates/rust-crypto rust-crypto] crate for sha256.
<syntaxhighlight lang=Rust>
<syntaxhighlight lang="rust">
extern crate crypto;
extern crate crypto;


Line 2,121: Line 2,092:
false
false
</pre>
</pre>

=={{header|Scala}}==
=={{header|Scala}}==
<syntaxhighlight lang=Scala>import java.security.MessageDigest
<syntaxhighlight lang="scala">import java.security.MessageDigest
import java.util.Arrays.copyOfRange
import java.util.Arrays.copyOfRange


Line 2,190: Line 2,160:


}</syntaxhighlight>
}</syntaxhighlight>

=={{header|Seed7}}==
=={{header|Seed7}}==
The Seed7 library [http://seed7.sourceforge.net/libraries/encoding.htm encoding.s7i] defines
The Seed7 library [http://seed7.sourceforge.net/libraries/encoding.htm encoding.s7i] defines
Line 2,200: Line 2,169:
No external library is needed.
No external library is needed.


<syntaxhighlight lang=seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "msgdigest.s7i";
include "msgdigest.s7i";
include "encoding.s7i";
include "encoding.s7i";
Line 2,273: Line 2,242:
16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM: TRUE
16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM: TRUE
</pre>
</pre>

=={{header|Tcl}}==
=={{header|Tcl}}==
{{tcllib|sha256}}
{{tcllib|sha256}}
<syntaxhighlight lang=tcl>package require sha256
<syntaxhighlight lang="tcl">package require sha256


# Generate a large and boring piece of code to do the decoding of
# Generate a large and boring piece of code to do the decoding of
Line 2,311: Line 2,279:
}</syntaxhighlight>
}</syntaxhighlight>
Testing if it works
Testing if it works
<syntaxhighlight lang=tcl>puts [bitcoin_addressValid 1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9]
<syntaxhighlight lang="tcl">puts [bitcoin_addressValid 1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9]
puts [bitcoin_addressValid 1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i]</syntaxhighlight>
puts [bitcoin_addressValid 1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i]</syntaxhighlight>
{{out}}
{{out}}
Line 2,318: Line 2,286:
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i is ok
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i is ok
</pre>
</pre>

=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==
{{works with|bash}}
{{works with|bash}}
<syntaxhighlight lang=bash>base58=({1..9} {A..H} {J..N} {P..Z} {a..k} {m..z})
<syntaxhighlight lang="bash">base58=({1..9} {A..H} {J..N} {P..Z} {a..k} {m..z})
bitcoinregex="^[$(printf "%s" "${base58[@]}")]{34}$"
bitcoinregex="^[$(printf "%s" "${base58[@]}")]{34}$"


Line 2,349: Line 2,316:
fi
fi
}</syntaxhighlight>
}</syntaxhighlight>

=={{header|Wren}}==
=={{header|Wren}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
Line 2,355: Line 2,321:
{{libheader|wren-str}}
{{libheader|wren-str}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang=ecmascript>import "/crypto" for Sha256
<syntaxhighlight lang="ecmascript">import "/crypto" for Sha256
import "/str" for Str
import "/str" for Str
import "/fmt" for Conv, Fmt
import "/fmt" for Conv, Fmt
Line 2,434: Line 2,400:
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I -> invalid
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I -> invalid
</pre>
</pre>

=={{header|zkl}}==
=={{header|zkl}}==
Uses shared library zklMsgHash.
Uses shared library zklMsgHash.
<syntaxhighlight lang=zkl>var [const] MsgHash=Import("zklMsgHash"); // SHA-256, etc
<syntaxhighlight lang="zkl">var [const] MsgHash=Import("zklMsgHash"); // SHA-256, etc
const symbols="123456789" // 58 characters: no cap i,o; ell, zero
const symbols="123456789" // 58 characters: no cap i,o; ell, zero
"ABCDEFGHJKLMNPQRSTUVWXYZ"
"ABCDEFGHJKLMNPQRSTUVWXYZ"
Line 2,462: Line 2,427:
dec[0,4]==chkSum;
dec[0,4]==chkSum;
}</syntaxhighlight>
}</syntaxhighlight>
<syntaxhighlight lang=zkl>T("1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i","1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9",
<syntaxhighlight lang="zkl">T("1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i","1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9",
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62X", // checksum changed, original data.
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62X", // checksum changed, original data.
"1ANNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i", // data changed, original checksum.
"1ANNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i", // data changed, original checksum.
Line 2,470: Line 2,435:
{{out}}
{{out}}
<pre>L(True,True,False,False,False,False)</pre>
<pre>L(True,True,False,False,False,False)</pre>
{{omit from|Brlcad}}
{{omit from|GUISS}}