Add ClassMethods

This commit is contained in:
Didactic Drunk 2021-06-14 16:50:32 -07:00
parent 1008e68035
commit 4a32468d7d
5 changed files with 25 additions and 0 deletions

View File

@ -27,4 +27,10 @@ describe Crypto::Secret::Not do
secret.to_s.should_not match /Bytes|Slice/
secret.inspect.should_not match /Bytes|Slice/
end
it "returns a random secret" do
secret1 = Crypto::Secret::Not.new 8
secret2 = Crypto::Secret::Not.random 8
secret1.should_not eq secret2
end
end

View File

@ -0,0 +1,10 @@
module Crypto::Secret::ClassMethods
# Returns a **readonly** random Secret
def random(size)
buf = new(size)
buf.readwrite do |slice|
Random::Secure.random_bytes slice
end
buf.readonly
end
end

View File

@ -1,4 +1,5 @@
require "./lib"
require "./class_methods"
# Interface to hold sensitive information (often cryptographic keys)
#

View File

@ -12,6 +12,10 @@ module Crypto::Secret
module Stateful
include Crypto::Secret
macro included
extend ClassMethods
end
@state = State::Readwrite
# Temporarily make buffer readwrite within the block returning to the prior state on exit.

View File

@ -6,6 +6,10 @@ require "./secret"
module Crypto::Secret::Stateless
include Crypto::Secret
macro included
extend ClassMethods
end
# Not thread safe
def readwrite
end