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



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

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



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

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



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

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
# 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|
        size = _decode_decrypt(buff_ptr, text_ptr, text_len, message_id, context_ptr, key_ptr)
        result = buff_ptr.get_bytes(0, size)
      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)
      end
    end
  end

  result
end

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



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

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



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

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



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

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



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

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)
  end

  decoded
end

.modp_b64_decode_len(len) ⇒ Object



110
111
112
# File 'lib/ffi/hydrogen.rb', line 110

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

.modp_b64_encode(text) ⇒ Object



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

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)
  end

  encoded
end

.modp_b64_encode_len(len) ⇒ Object



106
107
108
# File 'lib/ffi/hydrogen.rb', line 106

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