Module: AEZ
- Extended by:
- FFI::Library
- Defined in:
- lib/aez.rb,
lib/aez/version.rb
Overview
AEZv5 ruby binding. [AEZv5](web.cs.ucdavis.edu/~rogaway/aez)
Defined Under Namespace
Classes: Error
Constant Summary collapse
- MAX_CIPHER_TXT_LENGTH =
2**32 - 1
- VERSION =
'0.1.5'
Class Method Summary collapse
-
.decrypt(key, ciphertxt, ad, nonce, abyte) ⇒ String
Decrypt a message.
-
.encrypt(key, message, ad, nonce, abyte) ⇒ String
Encrypt a message.
- .with_context(key) {|context| ... } ⇒ Object
Class Method Details
.decrypt(key, ciphertxt, ad, nonce, abyte) ⇒ String
Decrypt a message.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/aez.rb', line 62 def decrypt(key, ciphertxt, ad, nonce, abyte) raise Error, 'invalid nonce.' if nonce.empty? || nonce.bytesize > 16 raise Error, 'ciphertxt length too long.' unless ciphertxt.bytesize < MAX_CIPHER_TXT_LENGTH with_context(key) do |context| ciphertxt_m = FFI::MemoryPointer.new(:uchar, ciphertxt.bytesize).put_bytes(0, ciphertxt) ad_m = ad.empty? ? nil : FFI::MemoryPointer.new(:char, ad.bytesize).put_bytes(0, ad) nonce_m = FFI::MemoryPointer.new(:char, nonce.bytesize).put_bytes(0, nonce) dest = FFI::MemoryPointer.new(:char, ciphertxt.bytesize - abyte) result = aez_decrypt(context, nonce_m, nonce.bytesize, ad_m, ad.bytesize, abyte, ciphertxt_m, ciphertxt.bytesize, dest) raise Error, 'decrypt failure.' unless result == 0 dest.read_string(ciphertxt.bytesize - abyte) end end |
.encrypt(key, message, ad, nonce, abyte) ⇒ String
Encrypt a message. these extra bytes add authentication.
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/aez.rb', line 41 def encrypt(key, , ad, nonce, abyte) raise Error, 'invalid nonce.' if nonce.empty? || nonce.bytesize > 16 with_context(key) do |context| = .empty? ? nil : FFI::MemoryPointer.new(:uchar, .bytesize).put_bytes(0, ) ad_m = ad.empty? ? nil : FFI::MemoryPointer.new(:char, ad.bytesize).put_bytes(0, ad) nonce_m = FFI::MemoryPointer.new(:char, nonce.bytesize).put_bytes(0, nonce) dest = FFI::MemoryPointer.new(:char, .bytesize + abyte) aez_encrypt(context, nonce_m, nonce.bytesize, ad_m, ad.bytesize, abyte, , .bytesize, dest) dest.read_string(.bytesize + abyte) end end |
.with_context(key) {|context| ... } ⇒ Object
78 79 80 81 82 83 |
# File 'lib/aez.rb', line 78 def with_context(key) context = FFI::MemoryPointer.new(144) key_m = FFI::MemoryPointer.new(:uchar, key.bytesize).put_bytes(0, key) aez_setup(key_m, key.bytesize, context) yield(context) if block_given? end |