Class: Lightning::Onion::ChaCha20::Pure

Inherits:
Object
  • Object
show all
Defined in:
lib/lightning/onion/chacha20/pure.rb

Class Method Summary collapse

Class Method Details

.chacha20_encrypt(key, counter, nonce, plaintext) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/lightning/onion/chacha20/pure.rb', line 8

def self.chacha20_encrypt(key, counter, nonce, plaintext)
  encrypted_message = +''
  (plaintext.length / 64).times do |i|
    key_stream = chacha20_block(key, counter + i, nonce)
    block = plaintext[(i * 64)...(i + 1) * 64]
    encrypted_message += xor(block, key_stream)
  end
  if plaintext.length % 64 != 0
    i = plaintext.length / 64
    key_stream = chacha20_block(key, counter + i, nonce)
    block = plaintext[(i * 64)...plaintext.length]
    block = block.ljust(64, "\x00")
    encrypted_message += xor(block, key_stream)[0...(plaintext.length % 64)]
  end
  encrypted_message
end