Add ClassMethods
This commit is contained in:
parent
1008e68035
commit
4a32468d7d
@ -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
|
||||
|
10
src/crypto-secret/class_methods.cr
Normal file
10
src/crypto-secret/class_methods.cr
Normal 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
|
@ -1,4 +1,5 @@
|
||||
require "./lib"
|
||||
require "./class_methods"
|
||||
|
||||
# Interface to hold sensitive information (often cryptographic keys)
|
||||
#
|
||||
|
@ -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.
|
||||
|
@ -6,6 +6,10 @@ require "./secret"
|
||||
module Crypto::Secret::Stateless
|
||||
include Crypto::Secret
|
||||
|
||||
macro included
|
||||
extend ClassMethods
|
||||
end
|
||||
|
||||
# Not thread safe
|
||||
def readwrite
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user