mirror of
https://github.com/neocities/neocities.git
synced 2025-08-06 01:24:56 +02:00
fix for archiver
This commit is contained in:
parent
1b6e12a831
commit
a9380d4554
4 changed files with 55 additions and 1 deletions
41
ext/base58.rb
Normal file
41
ext/base58.rb
Normal file
|
@ -0,0 +1,41 @@
|
|||
module Base58
|
||||
class << self
|
||||
def int_to_base58(int_val, leading_zero_bytes=0)
|
||||
alpha = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
|
||||
base58_val, base = '', alpha.size
|
||||
while int_val > 0
|
||||
int_val, remainder = int_val.divmod(base)
|
||||
base58_val = alpha[remainder] + base58_val
|
||||
end
|
||||
base58_val
|
||||
end
|
||||
|
||||
def base58_to_int(base58_val)
|
||||
alpha = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
|
||||
int_val, base = 0, alpha.size
|
||||
base58_val.reverse.each_char.with_index do |char,index|
|
||||
raise ArgumentError, 'Value not a valid Base58 String.' unless char_index = alpha.index(char)
|
||||
int_val += char_index*(base**index)
|
||||
end
|
||||
int_val
|
||||
end
|
||||
|
||||
def base58_to_bytestring(base58_val)
|
||||
[Base58.decode_base58(base58_val)].pack('H*')
|
||||
end
|
||||
|
||||
def encode_base58(hex)
|
||||
leading_zero_bytes = (hex.match(/^([0]+)/) ? $1 : '').size / 2
|
||||
("1"*leading_zero_bytes) + int_to_base58( hex.to_i(16) )
|
||||
end
|
||||
|
||||
def decode_base58(base58_val)
|
||||
s = base58_to_int(base58_val).to_s(16); s = (s.bytesize.odd? ? '0'+s : s)
|
||||
s = '' if s == '00'
|
||||
leading_zero_bytes = (base58_val.match(/^([1]+)/) ? $1 : '').size
|
||||
s = ("00"*leading_zero_bytes) + s if leading_zero_bytes > 0
|
||||
s
|
||||
end
|
||||
alias_method :base58_to_hex, :decode_base58
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue