module Sodium
Constants
- CryptoError
- LengthError
- MemoryError
- VERSION
Public Instance Methods
allocarray(count, size)
click to toggle source
# File lib/sodium.rb, line 49 def allocarray(count, size) if (mem = sodium_allocarray(count, size)).null? raise NoMemoryError, "Failed to allocate memory size=#{count * size} bytes", caller end mem end
bin2hex(bin)
click to toggle source
# File lib/sodium.rb, line 57 def bin2hex(bin) bin_len = get_size(bin) hex = zeros(bin_len * 2 + 1) sodium_bin2hex(hex, hex.bytesize, bin, bin_len) end
hex2bin(hex, bin_maxlen, ignore = nil)
click to toggle source
# File lib/sodium.rb, line 63 def hex2bin(hex, bin_maxlen, ignore = nil) bin = zeros(bin_maxlen) bin_len = FFI::MemoryPointer.new(:size_t) if sodium_hex2bin(bin, bin_maxlen, hex, hex.bytesize, ignore, bin_len, nil) == -1 raise LengthError, "bin_maxlen=#{bin_maxlen} is too short", caller end size = bin_len.size == 8 ? bin_len.get_uint64(0) : bin_len.get_uint32(0) [bin, size] end
hex2bin!(hex, bin_maxlen, ignore = nil)
click to toggle source
# File lib/sodium.rb, line 74 def hex2bin!(hex, bin_maxlen, ignore = nil) bin_len = FFI::MemoryPointer.new(:size_t) if sodium_hex2bin(hex, bin_maxlen, hex, hex.bytesize, ignore, bin_len, nil) == -1 raise LengthError, "bin_maxlen=#{bin_maxlen} is too short", caller end size = bin_len.size == 8 ? bin_len.get_uint64(0) : bin_len.get_uint32(0) hex.slice!(size..-1) hex end
malloc(size)
click to toggle source
# File lib/sodium.rb, line 41 def malloc(size) if (mem = sodium_malloc(size)).null? raise NoMemoryError, "Failed to allocate memory size=#{size} bytes", caller end mem end
mlock(addr, len)
click to toggle source
# File lib/sodium.rb, line 25 def mlock(addr, len) if sodium_mlock(addr, len) == -1 raise MemoryError, "Could not lock length=#{len} bytes memory at address=#{addr.address}", caller end true end
munlock(addr, len)
click to toggle source
# File lib/sodium.rb, line 33 def munlock(addr, len) if sodium_munlock(addr, len) == -1 raise MemoryError, "Could not unlock length=#{len} bytes memory at address=#{addr.address}", caller end true end