Module: Crypto
- Defined in:
- lib/crypto-lite.rb,
lib/crypto-lite/metal.rb,
lib/crypto-lite/config.rb,
lib/crypto-lite/sign_rsa.rb
Defined Under Namespace
Modules: Metal, RSA
Classes: Configuration
Constant Summary
collapse
- HEX_RE =
more helpers check if it is a hex (string)
- allow optiona 0x or 0X and allow abcdef and ABCDEF
/\A(?:0x)?[0-9a-f]+\z/i
Class Method Summary
collapse
Class Method Details
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/crypto-lite.rb', line 90
def self.args_to_input( args, kwargs )
if kwargs[:hex]
hex = kwargs[:hex]
raise ArgumentError, "expected hex string (0-9a-f) - got >#{hex}< - can't pack string; sorry" unless hex =~ HEX_RE
hex = strip0x( hex )
[hex].pack( 'H*' )
else
input = args[0]
input = hex_to_bin_automagic( input )
input
end
end
|
.base58(*args, **kwargs) ⇒ Object
28
29
30
31
|
# File 'lib/crypto-lite.rb', line 28
def self.base58( *args, **kwargs )
input = args_to_input( args, kwargs )
Metal.base58bin( input )
end
|
.base58check(*args, **kwargs) ⇒ Object
33
34
35
36
|
# File 'lib/crypto-lite.rb', line 33
def self.base58check( *args, **kwargs )
input = args_to_input( args, kwargs )
Metal.base58bin_check( input )
end
|
.configuration ⇒ Object
lets you use
Crypto.configure do |config|
config.debug = true
end
18
19
20
|
# File 'lib/crypto-lite/config.rb', line 18
def self.configuration
@configuration ||= Configuration.new
end
|
22
23
24
|
# File 'lib/crypto-lite/config.rb', line 22
def self.configure
yield( configuration )
end
|
.debug=(value) ⇒ Object
28
|
# File 'lib/crypto-lite/config.rb', line 28
def self.debug=(value) self.configuration.debug = value; end
|
.debug? ⇒ Boolean
add convenience helper for format
27
|
# File 'lib/crypto-lite/config.rb', line 27
def self.debug?() configuration.debug?; end
|
.hash160(*args, **kwargs) ⇒ Object
72
73
74
75
|
# File 'lib/crypto-lite.rb', line 72
def self.hash160( *args, **kwargs )
input = args_to_input( args, kwargs )
Metal.hash160bin( input ).unpack( 'H*' )[0]
end
|
.hash256(*args, **kwargs) ⇒ Object
77
78
79
80
|
# File 'lib/crypto-lite.rb', line 77
def self.hash256( *args, **kwargs )
input = args_to_input( args, kwargs )
Metal.hash256bin( input ).unpack( 'H*' )[0]
end
|
.hex_to_bin_automagic(input) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/crypto-lite.rb', line 104
def self.hex_to_bin_automagic( input )
if input.is_a?( String ) && input =~ HEX_RE
if input[0,2] == '0x' || input[0,2] == '0X'
input = input[2..-1]
[input].pack( 'H*' )
elsif input.size >= 10
[input].pack( 'H*' )
else
input
end
else
input
end
end
|
.keccak256(*args, **kwargs) ⇒ Object
42
43
44
45
|
# File 'lib/crypto-lite.rb', line 42
def self.keccak256( *args, **kwargs )
input = args_to_input( args, kwargs )
Metal.keccak256bin( input ).unpack( 'H*' )[0]
end
|
.rmd160(*args, **kwargs) ⇒ Object
Also known as:
ripemd160
48
49
50
51
|
# File 'lib/crypto-lite.rb', line 48
def self.rmd160( *args, **kwargs )
input = args_to_input( args, kwargs )
Metal.rmd160bin( input ).unpack( 'H*' )[0]
end
|
.sha256(*args, **kwargs) ⇒ Object
59
60
61
62
63
|
# File 'lib/crypto-lite.rb', line 59
def self.sha256( *args, **kwargs )
input = args_to_input( args, kwargs )
engine = kwargs[:engine]
Metal.sha256bin( input, engine ).unpack( 'H*' )[0]
end
|
.sha3_256(*args, **kwargs) ⇒ Object
65
66
67
68
|
# File 'lib/crypto-lite.rb', line 65
def self.sha3_256( *args, **kwargs )
input = args_to_input( args, kwargs )
Metal.sha3_256bin( input ).unpack( 'H*' )[0]
end
|
.strip0x(str) ⇒ Object
todo/check: add alias e.g. strip_hex_prefix or such - why? why not?
124
125
126
|
# File 'lib/crypto-lite.rb', line 124
def self.strip0x( str )
(str[0,2] == '0x' || str[0,2] == '0X') ? str[2..-1] : str
end
|