Class: Sodium::Box

Inherits:
Object
  • Object
show all
Includes:
Delegate
Defined in:
lib/sodium/box.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Delegate

class_methods, included, #primitive

Constructor Details

#initialize(secret_key, public_key) ⇒ Box

Returns a new instance of Box.



51
52
53
54
# File 'lib/sodium/box.rb', line 51

def initialize(secret_key, public_key)
  @secret_key = self.class._secret_key(secret_key)
  @public_key = self.class._public_key(public_key)
end

Class Method Details

.afternm(shared_key, message, nonce) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sodium/box.rb', line 18

def self.afternm(shared_key, message, nonce)
  shared_key = _shared_key(shared_key)
  message    = _message(message)
  nonce      = _nonce(nonce)

  Sodium::Buffer.empty(message.bytesize) do |ciphertext|
    self.implementation.nacl_afternm(
      ciphertext.to_ptr,
      message   .to_ptr,
      message   .bytesize,
      nonce     .to_ptr,
      shared_key.to_ptr
    ) or raise Sodium::CryptoError, 'failed to close the box'
  end.ldrop self.implementation[:BOXZEROBYTES]
end

.keypairObject



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/sodium/box.rb', line 6

def self.keypair
  public_key = Sodium::Buffer.empty self.implementation[:PUBLICKEYBYTES]
  secret_key = Sodium::Buffer.empty self.implementation[:SECRETKEYBYTES]

  self.implementation.nacl_keypair(
    public_key.to_ptr,
    secret_key.to_ptr
  ) or raise Sodium::CryptoError, 'failed to generate a keypair'

  return secret_key, public_key
end

.open_afternm(shared_key, ciphertext, nonce) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sodium/box.rb', line 34

def self.open_afternm(shared_key, ciphertext, nonce)
  shared_key = _shared_key(shared_key)
  ciphertext = _ciphertext(ciphertext)
  nonce      = _nonce(nonce)

  Sodium::Buffer.empty(ciphertext.bytesize) do |message|
    self.implementation.nacl_open_afternm(
      message   .to_ptr,
      ciphertext.to_ptr,
      ciphertext.bytesize,
      nonce     .to_ptr,
      shared_key.to_ptr
    ) or raise Sodium::CryptoError, 'failed to open the box'
  end.ldrop self.implementation[:ZEROBYTES]
end

Instance Method Details

#beforenmObject



92
93
94
95
96
97
98
99
100
# File 'lib/sodium/box.rb', line 92

def beforenm
  Sodium::Buffer.empty self.implementation[:BEFORENMBYTES] do |shared_key|
    self.implementation.nacl_beforenm(
      shared_key .to_ptr,
      @public_key.to_ptr,
      @secret_key.to_ptr
    ) or raise Sodium::CryptoError, 'failed to create a shared key'
  end
end

#box(message, nonce) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/sodium/box.rb', line 60

def box(message, nonce)
  message = self.class._message(message)
  nonce   = self.class._nonce(nonce)

  Sodium::Buffer.empty(message.bytesize) do |ciphertext|
    self.implementation.nacl(
      ciphertext .to_ptr,
      message    .to_ptr,
      message    .bytesize,
      nonce      .to_ptr,
      @public_key.to_ptr,
      @secret_key.to_ptr
    ) or raise Sodium::CryptoError, 'failed to close the box'
  end.ldrop self.implementation[:BOXZEROBYTES]
end

#nonceObject



56
57
58
# File 'lib/sodium/box.rb', line 56

def nonce
  Sodium::Buffer.nonce self.implementation[:NONCEBYTES]
end

#open(ciphertext, nonce) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/sodium/box.rb', line 76

def open(ciphertext, nonce)
  ciphertext = self.class._ciphertext(ciphertext)
  nonce      = self.class._nonce(nonce)

  Sodium::Buffer.empty(ciphertext.bytesize) do |message|
    self.implementation.nacl_open(
      message    .to_ptr,
      ciphertext .to_ptr,
      ciphertext .bytesize,
      nonce      .to_ptr,
      @public_key.to_ptr,
      @secret_key.to_ptr
    ) or raise Sodium::CryptoError, 'failed to open the box'
  end.ldrop self.implementation[:ZEROBYTES]
end