Bitcoin/address validation: Difference between revisions
Content deleted Content added
Thundergnat (talk | contribs) m syntax highlighting fixup automation |
Thundergnat (talk | contribs) m Automated syntax highlighting fixup (second round - minor fixes) |
||
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}}
{{task}}
▲[[Category:Checksums]]
{{omit from|Brlcad}}▼
{{omit from|GUISS}}▼
Line 38 ⟶ 36:
<br>You can change a few characters in this string and check that it'll fail the test.
<br><br>
=={{header|Ada}}==
<syntaxhighlight lang="ada">with Ada.Exceptions, Interfaces;
with Ada.Streams;
use Ada.Exceptions, Interfaces;
Line 177 ⟶ 174:
1A Na15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i validity: *** Error: Invalid BT address.
</pre>
=={{header|C}}==
<syntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
#include <openssl/sha.h>
Line 248 ⟶ 244:
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I: bad char
</pre>
=={{header|C sharp|C#}}==
This requires [https://www.nuget.org/packages/NUnit/ NUnit package] to compile.
<syntaxhighlight lang="csharp">
using System;
using System.Linq;
Line 322 ⟶ 317:
}
</syntaxhighlight>
=={{header|D}}==
This requires the D module from the SHA-256 Task.
{{trans|Go}}
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.array, std.string, sha_256_2;
struct A25 {
Line 417 ⟶ 411:
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62iz": not Bitcoin version 0.
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62izz": too long Bitcoin address.</pre>
=={{header|dart}}==
This requires [https://pub.dev/packages/crypto Crypto package] to compile.
{{trans|Java}}
<syntaxhighlight lang="dart">import 'package:crypto/crypto.dart';
class Bitcoin {
Line 524 ⟶ 517:
"1BNGaR29FmfAqidXmD9HLws" false
</pre>
=={{header|Delphi}}==
This requires [http://www.cityinthesky.co.uk/opensource/DCPcrypt/ DCPcrypt library] to compile.
<syntaxhighlight lang="delphi">
uses
DCPsha256;
Line 593 ⟶ 585:
end;
</syntaxhighlight>
=={{header|Erlang}}==
Using base58 module from http://github.com/titan098/erl-base58.git.
<syntaxhighlight lang=
-module( bitcoin_address ).
Line 626 ⟶ 617:
in call from bitcoin_address:task/0 (src/bitcoin_address.erl, line 9)
</pre>
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: byte-arrays checksums checksums.sha io.binary kernel math
math.parser sequences ;
IN: rosetta-code.bitcoin.validation
Line 652 ⟶ 642:
"1ANNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i" btc-valid? . ! f, data changed, original checksum.
</pre>
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' version 05-04-2017
' compile with: fbc -s console
Line 866 ⟶ 855:
Bitcoin address: 0AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i first character is not 1 or 3
Bitcoin address: 1AGNa15ZQXAZUgFlqJ2i7Z2DPU2J6hW62i bitcoin address contains illegal character</pre>
=={{header|Go}}==
{{trans|C}}
<syntaxhighlight lang="go">package main
import (
Line 1,002 ⟶ 990:
1
</pre>
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">import Control.Monad (when)
import Data.List (elemIndex)
import Data.Monoid ((<>))
Line 1,071 ⟶ 1,058:
"1ANa55215ZQXAZUgFiqJ2i7Z2DPU2J6hW62i" -> "Address length exceeds 25 bytes"
"i55j" -> "Address length less than 4 bytes"</pre>
=={{header|Java}}==
<syntaxhighlight lang="java">import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
Line 1,142 ⟶ 1,128:
}
}</syntaxhighlight>
=={{header|Julia}}==
{{works with|Julia|0.6}}
{{trans|Python}}
<syntaxhighlight lang="julia">using SHA
bytes(n::Integer, l::Int) = collect(UInt8, (n >> 8i) & 0xFF for i in l-1:-1:0)
Line 1,212 ⟶ 1,197:
Address: 1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62iz
Expected: false Checked: false</pre>
=={{header|Kotlin}}==
{{trans|Java}}
<syntaxhighlight lang="scala">import java.security.MessageDigest
object Bitcoin {
Line 1,292 ⟶ 1,276:
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I -> invalid
</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang=
IntegerDigits[
FromDigits[
Line 1,310 ⟶ 1,293:
<pre>True
False</pre>
=={{header|Nim}}==
Tests on first digit character and on address length have been added to detect wrong addresses such as "BZbvjr".
Line 1,316 ⟶ 1,298:
===Using “libssl”===
<syntaxhighlight lang="nim">import algorithm
const SHA256Len = 32
Line 1,414 ⟶ 1,396:
===Using “nimcrypto”===
{{libheader|nimcrypto}}
<syntaxhighlight lang=
import strformat
Line 1,507 ⟶ 1,489:
Address “1111111111111111111114oLvT2” is valid.
Address “BZbvjr” is invalid (starting character is not 1 or 3).</pre>
=={{header|Oberon-2}}==
{{works with|oo2c}}{{libheader|Crypto}}
<syntaxhighlight lang="oberon2">
MODULE BitcoinAddress;
IMPORT
Line 1,600 ⟶ 1,581:
FALSE
</pre>
=={{header|Perl}}==
<syntaxhighlight lang="perl">my @b58 = qw{
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
Line 1,634 ⟶ 1,614:
substr sha256(sha256 pack 'C*', @byte[0..20]), 0, 4;
}</syntaxhighlight>
=={{header|Phix}}==
<!--<syntaxhighlight lang=
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\bitcoin_address_validation.exw
Line 1,722 ⟶ 1,701:
<!--</syntaxhighlight>-->
(No output other than "done" since all tests pass)
=={{header|PHP}}==
<syntaxhighlight lang="php">
function validate($address){
$decoded = decodeBase58($address);
Line 1,791 ⟶ 1,769:
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I: invalid character found
</pre>
=={{header|PicoLisp}}==
<syntaxhighlight lang=
(setq *Alphabet
Line 1,823 ⟶ 1,800:
(valid "1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62iz")
(valid "1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62izz") ) )</syntaxhighlight>
=={{header|PureBasic}}==
<syntaxhighlight lang=
; using PureBasic 5.50 (x64)
EnableExplicit
Line 1,911 ⟶ 1,887:
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I -> Invalid
</pre>
=={{header|Python}}==
<syntaxhighlight lang="python">from hashlib import sha256
digits58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
Line 1,940 ⟶ 1,915:
: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:
:<syntaxhighlight lang="python">>>> n = 2491969579123783355964723219455906992268673266682165637887
>>> length = 25
>>> list( reversed(range(length)) )
Line 1,946 ⟶ 1,921:
>>> assert n.to_bytes(length, 'big') == bytes( (n >> i*8) & 0xff for i in reversed(range(length)))
>>> </syntaxhighlight>
=={{header|Racket}}==
<syntaxhighlight lang="racket">
#lang racket/base
Line 1,994 ⟶ 1,968:
(validate-bitcoin-address "1badbadbadbadbadbadbadbadbadbadbad") ; => #f
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>sub sha256(blob8 $b) returns blob8 {
given run <openssl dgst -sha256 -binary>, :in, :out, :bin {
.in.write: $b;
Line 2,023 ⟶ 1,996:
{{out}}
<pre>「1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i」</pre>
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">
# Validate Bitcoin address
#
Line 2,054 ⟶ 2,026:
I calculate that it is c046b2ff
</pre>
=={{header|Rust}}==
This requires the [https://crates.io/crates/rust-crypto rust-crypto] crate for sha256.
<syntaxhighlight lang=
extern crate crypto;
Line 2,121 ⟶ 2,092:
false
</pre>
=={{header|Scala}}==
<syntaxhighlight lang=
import java.util.Arrays.copyOfRange
Line 2,190 ⟶ 2,160:
}</syntaxhighlight>
=={{header|Seed7}}==
The Seed7 library [http://seed7.sourceforge.net/libraries/encoding.htm encoding.s7i] defines
Line 2,200 ⟶ 2,169:
No external library is needed.
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "msgdigest.s7i";
include "encoding.s7i";
Line 2,273 ⟶ 2,242:
16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM: TRUE
</pre>
=={{header|Tcl}}==
{{tcllib|sha256}}
<syntaxhighlight lang="tcl">package require sha256
# Generate a large and boring piece of code to do the decoding of
Line 2,311 ⟶ 2,279:
}</syntaxhighlight>
Testing if it works
<syntaxhighlight lang="tcl">puts [bitcoin_addressValid 1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9]
puts [bitcoin_addressValid 1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i]</syntaxhighlight>
{{out}}
Line 2,318 ⟶ 2,286:
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i is ok
</pre>
=={{header|UNIX Shell}}==
{{works with|bash}}
<syntaxhighlight lang="bash">base58=({1..9} {A..H} {J..N} {P..Z} {a..k} {m..z})
bitcoinregex="^[$(printf "%s" "${base58[@]}")]{34}$"
Line 2,349 ⟶ 2,316:
fi
}</syntaxhighlight>
=={{header|Wren}}==
{{trans|Kotlin}}
Line 2,355 ⟶ 2,321:
{{libheader|wren-str}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascript">import "/crypto" for Sha256
import "/str" for Str
import "/fmt" for Conv, Fmt
Line 2,434 ⟶ 2,400:
1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I -> invalid
</pre>
=={{header|zkl}}==
Uses shared library zklMsgHash.
<syntaxhighlight lang="zkl">var [const] MsgHash=Import("zklMsgHash"); // SHA-256, etc
const symbols="123456789" // 58 characters: no cap i,o; ell, zero
"ABCDEFGHJKLMNPQRSTUVWXYZ"
Line 2,462 ⟶ 2,427:
dec[0,4]==chkSum;
}</syntaxhighlight>
<syntaxhighlight lang="zkl">T("1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i","1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9",
"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62X", // checksum changed, original data.
"1ANNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i", // data changed, original checksum.
Line 2,470 ⟶ 2,435:
{{out}}
<pre>L(True,True,False,False,False,False)</pre>
▲{{omit from|Brlcad}}
▲{{omit from|GUISS}}
|