-
Notifications
You must be signed in to change notification settings - Fork 6
/
tcls_verify_hexkey.awk
122 lines (110 loc) · 3.19 KB
/
tcls_verify_hexkey.awk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
##############################################################################
#
# awk script by Sven-Volker Nowarra
#
# Version by date comment
# 0.1 svn 14jul16 initial release
#
# Usage:
# echo "bitcoin hex key" | awk -f trx_verify_hexkey.awk
#
# Parameters:
# "bitcoin hex key": a hex string, that is verified against the rules
#
# verify if the provided parameter (the bitcoin private or public key in hex)
# is valid. The chars of a bitcoin hex key must be a part of these:
# [0123456789ABCDEFabcdef]
# The characteristics of a bitcoin hex key must be:
# - 64 chars for private keys
# - 66 chars for a COMPRESSED public key, beginning with "0x02" or "0x03"
# - 130 chars for an UNCOMPRESSED public key, beginning with "0x04"
#
# This is realized with awk, to stay a POSIX compliant, cause the different
# shell comparisons with regexp(s) are not really portable. Also code
# is more "readable" :-)
#
BEGIN {
hexchars="0123456789ABCDEFabcdef"
hexchars_offset=1
bitcoin_key_offset=1
found=0
}
##########################################################################
### function to validate characters of hexkey string ###
##########################################################################
function validate() {
# loop through the bitcoin key, fetch each char
while ( bitcoin_key_offset <= length($0) ) {
bitcoin_key_char = substr($1, bitcoin_key_offset, 1)
# loop through the hexchars, and see if bitcoin address char is included
while ( hexchars_offset <= length(hexchars) ) {
found=0
hexchar = substr(hexchars, hexchars_offset, 1)
if ( bitcoin_key_char == hexchar )
{
# printf " bitcoin_key_char %s = hexchar %s \n", bitcoin_key_char, hexchar
hexchars_offset=1
found=1
break
}
hexchars_offset=hexchars_offset+1
}
bitcoin_key_offset=bitcoin_key_offset+1
}
if ( found == 1 )
{
# printf " found valid bitcoin hex key string\n"
return 0
}
else
{
printf "*** Error: invalid bitcoin hex key string\n"
exit 1
}
}
##########################################################################
### AND HERE WE GO ... ###
##########################################################################
{
hex_key_len=length($0)
if ( hex_key_len == 64 )
{
validate()
exit 0
}
if ( hex_key_len == 66 )
{
# check, if first hex code is "02" or "03"
bitcoin_key_char = substr($1, bitcoin_key_offset, 2)
if (bitcoin_key_char != "02" && bitcoin_key_char != "03")
{
printf "*** Error: pubkey does not start with '0x02' or '0x03' \n"
exit 1
}
else
{
validate()
exit 0
}
}
if ( hex_key_len == 130 )
{
# check, if first hex code is "04"
bitcoin_key_char = substr($1, bitcoin_key_offset, 2)
if (bitcoin_key_char != "04")
{
printf "*** Error: pubkey does not start with '0x04' \n"
exit 1
}
else
{
validate()
exit 0
}
}
printf "*** Error: hex key string is of unknown length (%d) \n", hex_key_len
exit 1
}
# END {
# printf("\ndone ...")
# }