Module: FFI::Hydrogen

Extended by:
Library
Defined in:
lib/ffi/hydrogen.rb

Defined Under Namespace

Classes: Secretbox

Constant Summary collapse

KEYBYTES =

define hydro_secretbox_KEYBYTES 32 define hydro_secretbox_HEADERBYTES (20 + 16)

32
HEADERBYTES =
36

Class Method Summary collapse

Class Method Details

.create_context(context) ⇒ Object



167
168
169
170
171
172
173
174
# File 'lib/ffi/hydrogen.rb', line 167

def self.create_context(context)
  return yield(context) if context.is_a?(::FFI::MemoryPointer)

  ::FFI::MemoryPointer.new(:char, context.bytesize) do |context_ptr|
    context_ptr.put_bytes(0, context)
    yield(context_ptr)
  end
end

.create_key(key) ⇒ Object



176
177
178
179
180
181
182
183
# File 'lib/ffi/hydrogen.rb', line 176

def self.create_key(key)
  return yield(key) if key.is_a?(::FFI::MemoryPointer)

  ::FFI::MemoryPointer.new(:uint8, key.bytesize) do |key_ptr|
    key_ptr.put_bytes(0, key)
    yield(key_ptr)
  end
end

.create_string_and_buffer(text, buff_size) ⇒ Object



185
186
187
188
189
190
191
192
# File 'lib/ffi/hydrogen.rb', line 185

def self.create_string_and_buffer(text, buff_size)
  ::FFI::MemoryPointer.new(:char, buff_size) do |buff_ptr|
    ::FFI::MemoryPointer.new(:char, text.bytesize) do |text_ptr|
      text_ptr.put_bytes(0, text)
      yield(text_ptr, buff_ptr)
    end
  end
end

.decode_decrypt(text, context, key, message_id = 0) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ffi/hydrogen.rb', line 63

def self.decode_decrypt(text, context, key, message_id = 0)
  result = nil
  text_len = text.bytesize
  max_len = modp_b64_decode_len(text_len)

  create_key(key) do |key_ptr|
    create_context(context) do |context_ptr|
      create_string_and_buffer(text, max_len) do |text_ptr, buff_ptr|
        # binding.pry

        size = _decode_decrypt(buff_ptr, text_ptr, text_len, message_id, context_ptr, key_ptr)
        result = buff_ptr.get_bytes(0, size) if size != 0
      end
    end
  end

  result
end

.encrypt_encode(text, context, key, message_id = 0) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ffi/hydrogen.rb', line 46

def self.encrypt_encode(text, context, key, message_id = 0)
  result = nil
  text_len = text.bytesize
  max_len = modp_b64_encode_len(text_len + HEADERBYTES)

  create_key(key) do |key_ptr|
    create_context(context) do |context_ptr|
      create_string_and_buffer(text, max_len) do |text_ptr, buff_ptr|
        size = _encrypt_encode(buff_ptr, text_ptr, text_len, message_id, context_ptr, key_ptr)
        result = buff_ptr.get_bytes(0, size) if size != 0
      end
    end
  end

  result
end

.hydro_secretbox_decrypt(cipher_text, context, key, message_id = 0) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/ffi/hydrogen.rb', line 147

def self.hydro_secretbox_decrypt(cipher_text, context, key, message_id = 0)
  encrypted = nil
  cipher_len = cipher_text.bytesize - HEADERBYTES

  ::FFI::MemoryPointer.new(:char, cipher_len) do |text_ptr|
    ::FFI::MemoryPointer.new(:uint8, cipher_text.bytesize) do |cipher_ptr|
      cipher_ptr.put_bytes(0, cipher_text)

      create_context(context) do |context_ptr|
        create_key(key) do |key_ptr|
          ::FFI::Hydrogen._hydro_secretbox_decrypt(text_ptr, cipher_ptr, cipher_text.bytesize, message_id, context_ptr, key_ptr)
          encrypted = text_ptr.get_bytes(0, cipher_len)
        end
      end
    end
  end

  encrypted
end

.hydro_secretbox_encrypt(text, context, key, message_id = 0) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/ffi/hydrogen.rb', line 127

def self.hydro_secretbox_encrypt(text, context, key, message_id = 0)
  encrypted = nil
  cipher_len = HEADERBYTES + text.bytesize

  ::FFI::MemoryPointer.new(:uint8, cipher_len) do |cipher_ptr|
    ::FFI::MemoryPointer.new(:char, text.bytesize) do |text_ptr|
      text_ptr.put_bytes(0, text)

      create_context(context) do |context_ptr|
        create_key(key) do |key_ptr|
          ::FFI::Hydrogen._hydro_secretbox_encrypt(cipher_ptr, text_ptr, text.bytesize, message_id, context_ptr, key_ptr)
          encrypted = cipher_ptr.get_bytes(0, cipher_len)
        end
      end
    end
  end

  encrypted
end

.hydro_secretbox_keygenObject



116
117
118
119
120
121
122
123
124
125
# File 'lib/ffi/hydrogen.rb', line 116

def self.hydro_secretbox_keygen
  key = nil

  ::FFI::MemoryPointer.new(:char, KEYBYTES) do |buff|
    ::FFI::Hydrogen._hydro_secretbox_keygen(buff)
    key = buff.get_bytes(0, KEYBYTES)
  end

  key
end

.modp_b64_decode(text) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ffi/hydrogen.rb', line 95

def self.modp_b64_decode(text)
  decoded = nil
  text_len = text.bytesize
  buff_len = modp_b64_decode_len(text_len)

  create_string_and_buffer(text, buff_len) do |text_ptr, buff_ptr|
    size = ::FFI::Hydrogen._modp_b64_decode(buff_ptr, text_ptr, text_len)
    decoded = buff_ptr.get_bytes(0, size) if size != 0
  end

  decoded
end

.modp_b64_decode_len(len) ⇒ Object



112
113
114
# File 'lib/ffi/hydrogen.rb', line 112

def self.modp_b64_decode_len(len)
  (len / 4 * 3 + 2)
end

.modp_b64_encode(text) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ffi/hydrogen.rb', line 82

def self.modp_b64_encode(text)
  encoded = nil
  text_len = text.bytesize
  buff_len = modp_b64_encode_len(text_len)

  create_string_and_buffer(text, buff_len) do |text_ptr, buff_ptr|
    size = ::FFI::Hydrogen._modp_b64_encode(buff_ptr, text_ptr, text_len)
    encoded = buff_ptr.get_bytes(0, size) if size != 0
  end

  encoded
end

.modp_b64_encode_len(len) ⇒ Object



108
109
110
# File 'lib/ffi/hydrogen.rb', line 108

def self.modp_b64_encode_len(len)
  ((len + 2) / 3 * 4 + 1)
end