rbnacl 3.1.2 → 3.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +27 -0
  3. data/.travis.yml +4 -3
  4. data/CHANGES.md +5 -0
  5. data/Gemfile +8 -3
  6. data/Guardfile +1 -1
  7. data/README.md +5 -6
  8. data/Rakefile +3 -2
  9. data/lib/rbnacl.rb +1 -1
  10. data/lib/rbnacl/auth.rb +21 -8
  11. data/lib/rbnacl/boxes/curve25519xsalsa20poly1305.rb +17 -13
  12. data/lib/rbnacl/boxes/curve25519xsalsa20poly1305/private_key.rb +68 -65
  13. data/lib/rbnacl/boxes/curve25519xsalsa20poly1305/public_key.rb +49 -47
  14. data/lib/rbnacl/group_elements/curve25519.rb +14 -8
  15. data/lib/rbnacl/hash.rb +2 -2
  16. data/lib/rbnacl/hash/blake2b.rb +13 -13
  17. data/lib/rbnacl/hash/sha256.rb +5 -5
  18. data/lib/rbnacl/hash/sha512.rb +5 -5
  19. data/lib/rbnacl/hmac/sha256.rb +12 -11
  20. data/lib/rbnacl/hmac/sha512256.rb +11 -10
  21. data/lib/rbnacl/init.rb +1 -1
  22. data/lib/rbnacl/key_comparator.rb +3 -3
  23. data/lib/rbnacl/one_time_auths/poly1305.rb +4 -4
  24. data/lib/rbnacl/password_hash/scrypt.rb +10 -11
  25. data/lib/rbnacl/random.rb +2 -2
  26. data/lib/rbnacl/secret_boxes/xsalsa20poly1305.rb +28 -14
  27. data/lib/rbnacl/self_test.rb +17 -37
  28. data/lib/rbnacl/serializable.rb +9 -4
  29. data/lib/rbnacl/signatures/ed25519.rb +1 -0
  30. data/lib/rbnacl/signatures/ed25519/signing_key.rb +17 -9
  31. data/lib/rbnacl/signatures/ed25519/verify_key.rb +17 -6
  32. data/lib/rbnacl/simple_box.rb +6 -3
  33. data/lib/rbnacl/sodium.rb +6 -7
  34. data/lib/rbnacl/sodium/version.rb +3 -2
  35. data/lib/rbnacl/test_vectors.rb +57 -55
  36. data/lib/rbnacl/util.rb +12 -11
  37. data/lib/rbnacl/version.rb +3 -1
  38. data/rbnacl.gemspec +6 -8
  39. data/spec/rbnacl/authenticators/poly1305_spec.rb +1 -1
  40. data/spec/rbnacl/boxes/curve25519xsalsa20poly1305/private_key_spec.rb +1 -1
  41. data/spec/rbnacl/boxes/curve25519xsalsa20poly1305/public_key_spec.rb +1 -1
  42. data/spec/rbnacl/boxes/curve25519xsalsa20poly1305_spec.rb +2 -2
  43. data/spec/rbnacl/group_element_spec.rb +1 -1
  44. data/spec/rbnacl/hash/blake2b_spec.rb +1 -1
  45. data/spec/rbnacl/hash_spec.rb +1 -1
  46. data/spec/rbnacl/hmac/sha256_spec.rb +1 -1
  47. data/spec/rbnacl/hmac/sha512256_spec.rb +1 -1
  48. data/spec/rbnacl/password_hash/scrypt_spec.rb +3 -3
  49. data/spec/rbnacl/secret_box_spec.rb +3 -3
  50. data/spec/rbnacl/signatures/ed25519/signing_key_spec.rb +2 -2
  51. data/spec/rbnacl/signatures/ed25519/verify_key_spec.rb +12 -7
  52. data/spec/rbnacl/simple_box_spec.rb +2 -2
  53. data/spec/rbnacl/util_spec.rb +28 -29
  54. data/spec/shared/authenticator.rb +12 -12
  55. data/spec/shared/box.rb +2 -4
  56. data/spec/spec_helper.rb +11 -18
  57. data/tasks/rspec.rake +2 -2
  58. data/tasks/rubocop.rake +1 -1
  59. metadata +4 -5
  60. data/lib/rbnacl/rake_tasks.rb +0 -57
  61. data/tasks/ci.rake +0 -11
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fe8acfa46de2179b61050402d960a9d1e9a07422
4
- data.tar.gz: 5c2a37dd6ab6ff202331f467fc38ae94c879b256
3
+ metadata.gz: d579b906e40df02d066ed657b294a03258e973c1
4
+ data.tar.gz: efa52b89d6fa69c64e353bba805b54da1e4cd75e
5
5
  SHA512:
6
- metadata.gz: 00495b472d456ba7ddcccfab9350397ddb54b273747c3fe497e29f0fd6e17a676b6abd07cb095ff1a48154bd48c71e7fdb6025aa4cf82873ec65d3c9f6a18af7
7
- data.tar.gz: 17f5ce6876d8fd7331982df0135a489ecbee45d66382020b13d80139ea0ba642420ebf2eadc2e7cf9f6706e603e7a768215c3a921daa9775a28ff73c2f0b37f2
6
+ metadata.gz: bc23cabab38ee76841891e5f8ea39c864e2d3053ba70b2b27bce1a4f4699a9260ae20dba5134ecc5eb0264b4d177aae56938aaba8cf94ce20831cf99b3147f81
7
+ data.tar.gz: c5a70483933ff229e6ef2732d94032a95762c82e37c4fab8d02e551e2b4fc55db1e8706f67ee964d21ccd3f9a40bdc7454473891a1d982bd6479fef24b85e47f
@@ -0,0 +1,27 @@
1
+ #
2
+ # Style
3
+ #
4
+
5
+ # TODO: turn this down
6
+ LineLength:
7
+ Max: 150
8
+
9
+ Style/StringLiterals:
10
+ EnforcedStyle: double_quotes
11
+
12
+ Style/SingleSpaceBeforeFirstArg:
13
+ Enabled: false
14
+
15
+ Style/GlobalVars:
16
+ Enabled: false
17
+
18
+ #
19
+ # Metrics
20
+ #
21
+
22
+ Metrics/MethodLength:
23
+ CountComments: false
24
+ Max: 25
25
+
26
+ Metrics/AbcSize:
27
+ Max: 20
@@ -1,15 +1,16 @@
1
- script: "LD_LIBRARY_PATH=lib bundle exec rake ci"
1
+ script: bundle exec rake ci
2
2
 
3
3
  rvm:
4
- - 1.9.3
5
4
  - 2.0.0
6
- - 2.1.2
5
+ - 2.1.4
6
+ - 2.2.2
7
7
  - ruby-head
8
8
  - jruby
9
9
  - jruby-head
10
10
  - rbx-2
11
11
 
12
12
  matrix:
13
+ fast_finish: true
13
14
  allow_failures:
14
15
  - rvm: ruby-head
15
16
  - rvm: jruby-head
data/CHANGES.md CHANGED
@@ -1,3 +1,8 @@
1
+ 3.2.0 (2015-05-31)
2
+ ------------------
3
+ * Fix method signature for blake2b
4
+ * RuboCop-friendly codebase
5
+
1
6
  3.1.2 (2014-08-30)
2
7
  ------------------
3
8
  * Fix scrypt support with libsodium 0.7.0 (scryptsalsa208sha256)
data/Gemfile CHANGED
@@ -1,12 +1,17 @@
1
- source 'https://rubygems.org'
1
+ source "https://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in rbnacl.gemspec
4
4
  gemspec
5
5
 
6
6
  group :development do
7
- gem 'guard-rspec'
7
+ gem "guard-rspec"
8
8
  end
9
9
 
10
10
  group :test do
11
- gem 'coveralls', :require => false
11
+ gem "coveralls", require: false
12
+ gem "rbnacl-libsodium"
13
+ end
14
+
15
+ group :development, :test do
16
+ gem "rubocop"
12
17
  end
data/Guardfile CHANGED
@@ -4,5 +4,5 @@
4
4
  guard :rspec do
5
5
  watch(%r{^spec/.+_spec\.rb$})
6
6
  watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
7
- watch('spec/spec_helper.rb') { "spec" }
7
+ watch("spec/spec_helper.rb") { "spec" }
8
8
  end
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  ![RbNaCl](https://raw.github.com/cryptosphere/rbnacl/master/images/logo.png)
2
2
  ======
3
- [![Gem Version](https://badge.fury.io/rb/rbnacl.png)](http://badge.fury.io/rb/rbnacl)
4
- [![Build Status](https://travis-ci.org/cryptosphere/rbnacl.png?branch=master)](https://travis-ci.org/cryptosphere/rbnacl)
5
- [![Code Climate](https://codeclimate.com/github/cryptosphere/rbnacl.png)](https://codeclimate.com/github/cryptosphere/rbnacl)
6
- [![Coverage Status](https://coveralls.io/repos/cryptosphere/rbnacl/badge.png?branch=master)](https://coveralls.io/r/cryptosphere/rbnacl)
3
+ [![Gem Version](https://badge.fury.io/rb/rbnacl.svg)](http://badge.fury.io/rb/rbnacl)
4
+ [![Build Status](https://travis-ci.org/cryptosphere/rbnacl.svg?branch=master)](https://travis-ci.org/cryptosphere/rbnacl)
5
+ [![Code Climate](https://codeclimate.com/github/cryptosphere/rbnacl.svg)](https://codeclimate.com/github/cryptosphere/rbnacl)
6
+ [![Coverage Status](https://coveralls.io/repos/cryptosphere/rbnacl/badge.svg?branch=master)](https://coveralls.io/r/cryptosphere/rbnacl)
7
7
 
8
8
  A Ruby binding to the state-of-the-art [Networking and Cryptography][nacl]
9
9
  library by [Daniel J. Bernstein][djb]. This is **NOT** Google Native Client.
@@ -55,8 +55,7 @@ For more information on NaCl's goals, see Dan Bernstein's presentation
55
55
  You can use RbNaCl anywhere you can get libsodium installed (see below).
56
56
  RbNaCl is continuously integration tested on the following Ruby VMs:
57
57
 
58
- * MRI 2.0, 2.1
59
- * MRI 1.9 (YARV)
58
+ * MRI 2.0, 2.1, 2.2
60
59
  * JRuby 1.7 (in both 1.8/1.9 mode)
61
60
  * Rubinius HEAD (in both 1.8/1.9 mode)
62
61
 
data/Rakefile CHANGED
@@ -1,5 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+
2
3
  Dir[File.expand_path("../tasks/**/*.rake", __FILE__)].each { |task| load task }
3
- require File.expand_path("../lib/rbnacl/rake_tasks", __FILE__)
4
4
 
5
- task :default => %w(spec rubocop)
5
+ task default: %w(spec rubocop)
6
+ task ci: %w(spec rubocop)
@@ -11,6 +11,7 @@ require "rbnacl/simple_box"
11
11
  require "rbnacl/test_vectors"
12
12
  require "rbnacl/init"
13
13
 
14
+ # NaCl/libsodium for Ruby
14
15
  module RbNaCl
15
16
  # Oh no, something went wrong!
16
17
  #
@@ -37,7 +38,6 @@ module RbNaCl
37
38
  # The authenticator was forged or otherwise corrupt
38
39
  class BadAuthenticatorError < CryptoError; end
39
40
 
40
-
41
41
  # Public Key Encryption (Box): Curve25519XSalsa20Poly1305
42
42
  require "rbnacl/boxes/curve25519xsalsa20poly1305"
43
43
  require "rbnacl/boxes/curve25519xsalsa20poly1305/private_key"
@@ -1,6 +1,5 @@
1
1
  # encoding: binary
2
2
  module RbNaCl
3
-
4
3
  # Secret Key Authenticators
5
4
  #
6
5
  # These provide a means of verifying the integrity of a message, but only
@@ -71,7 +70,7 @@ module RbNaCl
71
70
  def verify(authenticator, message)
72
71
  auth = authenticator.to_s
73
72
  Util.check_length(auth, tag_bytes, "Provided authenticator")
74
- verify_message(auth, message) || raise(BadAuthenticatorError, "Invalid authenticator provided, message is corrupt")
73
+ verify_message(auth, message) || fail(BadAuthenticatorError, "Invalid authenticator provided, message is corrupt")
75
74
  end
76
75
 
77
76
  # The crypto primitive for this authenticator instance
@@ -84,25 +83,39 @@ module RbNaCl
84
83
  # The number of key bytes for this Auth class
85
84
  #
86
85
  # @return [Integer] number of key bytes
87
- def self.key_bytes; self::KEYBYTES; end
86
+ def self.key_bytes
87
+ self::KEYBYTES
88
+ end
88
89
 
89
90
  # The number of key bytes for this Auth instance
90
91
  #
91
92
  # @return [Integer] number of key bytes
92
- def key_bytes; self.class.key_bytes; end
93
+ def key_bytes
94
+ self.class.key_bytes
95
+ end
93
96
 
94
97
  # The number bytes in the tag or authenticator from this Auth class
95
98
  #
96
99
  # @return [Integer] number of tag bytes
97
- def self.tag_bytes; self::BYTES; end
100
+ def self.tag_bytes
101
+ self::BYTES
102
+ end
98
103
 
99
104
  # The number of bytes in the tag or authenticator for this Auth instance
100
105
  #
101
106
  # @return [Integer] number of tag bytes
102
- def tag_bytes; self.class.tag_bytes; end
107
+ def tag_bytes
108
+ self.class.tag_bytes
109
+ end
103
110
 
104
111
  private
105
- def compute_authenticator(authenticator, message); raise NotImplementedError; end
106
- def verify_message(authenticator, message); raise NotImplementedError; end
112
+
113
+ def compute_authenticator(_authenticator, _message)
114
+ fail NotImplementedError
115
+ end
116
+
117
+ def verify_message(_authenticator, _message)
118
+ fail NotImplementedError
119
+ end
107
120
  end
108
121
  end
@@ -80,7 +80,7 @@ module RbNaCl
80
80
  sodium_function :box_curve25519xsalsa20poly1305_open_afternm,
81
81
  :crypto_box_curve25519xsalsa20poly1305_open_afternm,
82
82
  [:pointer, :pointer, :ulong_long, :pointer, :pointer]
83
-
83
+
84
84
  sodium_function :box_curve25519xsalsa20poly1305_afternm,
85
85
  :crypto_box_curve25519xsalsa20poly1305_afternm,
86
86
  [:pointer, :pointer, :ulong_long, :pointer, :pointer]
@@ -97,9 +97,9 @@ module RbNaCl
97
97
  #
98
98
  # @return [RbNaCl::Box] The new Box, ready to use
99
99
  def initialize(public_key, private_key)
100
- @public_key = PublicKey === public_key ? public_key : PublicKey.new(public_key)
101
- @private_key = PrivateKey === private_key ? private_key : PrivateKey.new(private_key)
102
- raise IncorrectPrimitiveError unless @public_key.primitive == primitive && @private_key.primitive == primitive
100
+ @public_key = public_key.is_a?(PublicKey) ? public_key : PublicKey.new(public_key)
101
+ @private_key = private_key.is_a?(PrivateKey) ? private_key : PrivateKey.new(private_key)
102
+ fail IncorrectPrimitiveError unless @public_key.primitive == primitive && @private_key.primitive == primitive
103
103
  end
104
104
 
105
105
  # Encrypts a message
@@ -121,10 +121,10 @@ module RbNaCl
121
121
  msg = Util.prepend_zeros(ZEROBYTES, message)
122
122
  ct = Util.zeros(msg.bytesize)
123
123
 
124
- self.class.box_curve25519xsalsa20poly1305_afternm(ct, msg, msg.bytesize, nonce, beforenm) || raise(CryptoError, "Encryption failed")
124
+ self.class.box_curve25519xsalsa20poly1305_afternm(ct, msg, msg.bytesize, nonce, beforenm) || fail(CryptoError, "Encryption failed")
125
125
  Util.remove_zeros(BOXZEROBYTES, ct)
126
126
  end
127
- alias encrypt box
127
+ alias_method :encrypt, :box
128
128
 
129
129
  # Decrypts a ciphertext
130
130
  #
@@ -145,10 +145,12 @@ module RbNaCl
145
145
  ct = Util.prepend_zeros(BOXZEROBYTES, ciphertext)
146
146
  message = Util.zeros(ct.bytesize)
147
147
 
148
- self.class.box_curve25519xsalsa20poly1305_open_afternm(message, ct, ct.bytesize, nonce, beforenm) || raise(CryptoError, "Decryption failed. Ciphertext failed verification.")
148
+ success = self.class.box_curve25519xsalsa20poly1305_open_afternm(message, ct, ct.bytesize, nonce, beforenm)
149
+ fail CryptoError, "Decryption failed. Ciphertext failed verification." unless success
150
+
149
151
  Util.remove_zeros(ZEROBYTES, message)
150
152
  end
151
- alias decrypt open
153
+ alias_method :decrypt, :open
152
154
 
153
155
  # The crypto primitive for the box class
154
156
  #
@@ -172,12 +174,14 @@ module RbNaCl
172
174
  end
173
175
 
174
176
  private
177
+
175
178
  def beforenm
176
- @k ||= begin
177
- k = Util.zeros(BEFORENMBYTES)
178
- self.class.box_curve25519xsalsa20poly1305_beforenm(k, @public_key.to_s, @private_key.to_s) || raise(CryptoError, "Failed to derive shared key")
179
- k
180
- end
179
+ @_key ||= begin
180
+ key = Util.zeros(BEFORENMBYTES)
181
+ success = self.class.box_curve25519xsalsa20poly1305_beforenm(key, @public_key.to_s, @private_key.to_s)
182
+ fail CryptoError, "Failed to derive shared key" unless success
183
+ key
184
+ end
181
185
  end
182
186
  end
183
187
  end
@@ -1,79 +1,82 @@
1
1
  # encoding: binary
2
2
 
3
3
  module RbNaCl
4
- # RbNaCl::Box private key. Keep it safe
5
- #
6
- # This class generates and stores NaCL private keys, as well as providing a
7
- # reference to the public key associated with this private key, if that's
8
- # provided.
9
- #
10
- # Note that the documentation for NaCl refers to this as a secret key, but in
11
- # this library its a private key, to avoid confusing the issue with the
12
- # SecretBox, which does symmetric encryption.
13
- class Boxes::Curve25519XSalsa20Poly1305::PrivateKey
4
+ module Boxes
5
+ class Curve25519XSalsa20Poly1305
6
+ # RbNaCl::Box private key. Keep it safe
7
+ #
8
+ # This class generates and stores NaCL private keys, as well as providing a
9
+ # reference to the public key associated with this private key, if that's
10
+ # provided.
11
+ #
12
+ # Note that the documentation for NaCl refers to this as a secret key, but in
13
+ # this library its a private key, to avoid confusing the issue with the
14
+ # SecretBox, which does symmetric encryption.
15
+ class PrivateKey
16
+ include KeyComparator
17
+ include Serializable
14
18
 
15
- include KeyComparator
16
- include Serializable
17
-
18
- extend Sodium
19
+ extend Sodium
19
20
 
20
- sodium_type :box
21
- sodium_primitive :curve25519xsalsa20poly1305
21
+ sodium_type :box
22
+ sodium_primitive :curve25519xsalsa20poly1305
22
23
 
23
- sodium_function :box_curve25519xsalsa20poly1305_keypair,
24
- :crypto_box_curve25519xsalsa20poly1305_keypair,
25
- [:pointer, :pointer]
24
+ sodium_function :box_curve25519xsalsa20poly1305_keypair,
25
+ :crypto_box_curve25519xsalsa20poly1305_keypair,
26
+ [:pointer, :pointer]
26
27
 
27
- # The size of the key, in bytes
28
- BYTES = Boxes::Curve25519XSalsa20Poly1305::PRIVATEKEYBYTES
28
+ # The size of the key, in bytes
29
+ BYTES = Boxes::Curve25519XSalsa20Poly1305::PRIVATEKEYBYTES
29
30
 
30
- # Initializes a new PrivateKey for key operations.
31
- #
32
- # Takes the (optionally encoded) private key bytes. This class can then be
33
- # used for various key operations, including deriving the corresponding
34
- # PublicKey
35
- #
36
- # @param private_key [String] The private key
37
- #
38
- # @raise [TypeError] If the key is nil
39
- # @raise [RbNaCl::LengthError] If the key is not valid after decoding.
40
- #
41
- # @return A new PrivateKey
42
- def initialize(private_key)
43
- @private_key = Util.check_string(private_key, BYTES, "Private key")
44
- end
31
+ # Initializes a new PrivateKey for key operations.
32
+ #
33
+ # Takes the (optionally encoded) private key bytes. This class can then be
34
+ # used for various key operations, including deriving the corresponding
35
+ # PublicKey
36
+ #
37
+ # @param private_key [String] The private key
38
+ #
39
+ # @raise [TypeError] If the key is nil
40
+ # @raise [RbNaCl::LengthError] If the key is not valid after decoding.
41
+ #
42
+ # @return A new PrivateKey
43
+ def initialize(private_key)
44
+ @private_key = Util.check_string(private_key, BYTES, "Private key")
45
+ end
45
46
 
46
- # Generates a new keypair
47
- #
48
- # @raise [RbNaCl::CryptoError] if key generation fails, due to insufficient randomness.
49
- #
50
- # @return [RbNaCl::PrivateKey] A new private key, with the associated public key also set.
51
- def self.generate
52
- pk = Util.zeros(Boxes::Curve25519XSalsa20Poly1305::PUBLICKEYBYTES)
53
- sk = Util.zeros(Boxes::Curve25519XSalsa20Poly1305::PRIVATEKEYBYTES)
54
- self.box_curve25519xsalsa20poly1305_keypair(pk, sk) || raise(CryptoError, "Failed to generate a key pair")
55
- new(sk)
56
- end
47
+ # Generates a new keypair
48
+ #
49
+ # @raise [RbNaCl::CryptoError] if key generation fails, due to insufficient randomness.
50
+ #
51
+ # @return [RbNaCl::PrivateKey] A new private key, with the associated public key also set.
52
+ def self.generate
53
+ pk = Util.zeros(Boxes::Curve25519XSalsa20Poly1305::PUBLICKEYBYTES)
54
+ sk = Util.zeros(Boxes::Curve25519XSalsa20Poly1305::PRIVATEKEYBYTES)
55
+ box_curve25519xsalsa20poly1305_keypair(pk, sk) || fail(CryptoError, "Failed to generate a key pair")
56
+ new(sk)
57
+ end
57
58
 
58
- # The raw bytes of the key
59
- #
60
- # @return [String] the raw bytes.
61
- def to_bytes
62
- @private_key
63
- end
59
+ # The raw bytes of the key
60
+ #
61
+ # @return [String] the raw bytes.
62
+ def to_bytes
63
+ @private_key
64
+ end
64
65
 
65
- # the public key associated with this private key
66
- #
67
- # @return [PublicKey] the key
68
- def public_key
69
- @public_key ||= PublicKey.new GroupElements::Curve25519.base.mult(to_bytes)
70
- end
71
-
72
- # The crypto primitive this PrivateKey is to be used for.
73
- #
74
- # @return [Symbol] The primitive
75
- def primitive
76
- self.class.primitive
66
+ # the public key associated with this private key
67
+ #
68
+ # @return [PublicKey] the key
69
+ def public_key
70
+ @public_key ||= PublicKey.new GroupElements::Curve25519.base.mult(to_bytes)
71
+ end
72
+
73
+ # The crypto primitive this PrivateKey is to be used for.
74
+ #
75
+ # @return [Symbol] The primitive
76
+ def primitive
77
+ self.class.primitive
78
+ end
79
+ end
77
80
  end
78
81
  end
79
82
  end
@@ -1,53 +1,55 @@
1
1
  # encoding: binary
2
2
 
3
3
  module RbNaCl
4
- # RbNaCl::Box public key. Send it (securely!) to your friends.
5
- #
6
- # This class stores the NaCL public key, and provides some convenience
7
- # functions for working with it.
8
- class Boxes::Curve25519XSalsa20Poly1305::PublicKey
9
-
10
- include KeyComparator
11
- include Serializable
12
-
13
- # The size of the key, in bytes
14
- BYTES = Boxes::Curve25519XSalsa20Poly1305::PUBLICKEYBYTES
15
-
16
- # Initializes a new PublicKey for key operations.
17
- #
18
- # Takes the (optionally encoded) public key bytes. This can be shared with
19
- # many people and used to establish key pairs with their private key, for
20
- # the exchanging of messages using a RbNaCl::Box
21
- #
22
- # @param public_key [String] The public key
23
- #
24
- # @raise [RbNaCl::LengthError] If the key is not valid after decoding.
25
- #
26
- # @return A new PublicKey
27
- def initialize(public_key)
28
- @public_key = Util.check_string(public_key, BYTES, "Public key")
29
- end
30
-
31
- # The raw bytes of the key
32
- #
33
- # @return [String] the raw bytes.
34
- def to_bytes
35
- @public_key
36
- end
37
-
38
- # The crypto primitive the PublicKey class is to be used for
39
- #
40
- # @return [Symbol] The primitive
41
- def self.primitive
42
- :curve25519xsalsa20poly1305
43
- end
44
-
45
- # The crypto primitive this PublicKey is to be used for.
46
- #
47
- # @return [Symbol] The primitive
48
- def primitive
49
- self.class.primitive
4
+ module Boxes
5
+ class Curve25519XSalsa20Poly1305
6
+ # RbNaCl::Box public key. Send it (securely!) to your friends.
7
+ #
8
+ # This class stores the NaCL public key, and provides some convenience
9
+ # functions for working with it.
10
+ class PublicKey
11
+ include KeyComparator
12
+ include Serializable
13
+
14
+ # The size of the key, in bytes
15
+ BYTES = Boxes::Curve25519XSalsa20Poly1305::PUBLICKEYBYTES
16
+
17
+ # Initializes a new PublicKey for key operations.
18
+ #
19
+ # Takes the (optionally encoded) public key bytes. This can be shared with
20
+ # many people and used to establish key pairs with their private key, for
21
+ # the exchanging of messages using a RbNaCl::Box
22
+ #
23
+ # @param public_key [String] The public key
24
+ #
25
+ # @raise [RbNaCl::LengthError] If the key is not valid after decoding.
26
+ #
27
+ # @return A new PublicKey
28
+ def initialize(public_key)
29
+ @public_key = Util.check_string(public_key, BYTES, "Public key")
30
+ end
31
+
32
+ # The raw bytes of the key
33
+ #
34
+ # @return [String] the raw bytes.
35
+ def to_bytes
36
+ @public_key
37
+ end
38
+
39
+ # The crypto primitive the PublicKey class is to be used for
40
+ #
41
+ # @return [Symbol] The primitive
42
+ def self.primitive
43
+ :curve25519xsalsa20poly1305
44
+ end
45
+
46
+ # The crypto primitive this PublicKey is to be used for.
47
+ #
48
+ # @return [Symbol] The primitive
49
+ def primitive
50
+ self.class.primitive
51
+ end
52
+ end
50
53
  end
51
54
  end
52
-
53
55
  end