Secret: #readonly, #readwrite, #noaccess add abstract & type restrictions

This commit is contained in:
Didactic Drunk 2021-06-21 17:12:57 -07:00
parent df18ae2bf0
commit f9eefbde0e
3 changed files with 16 additions and 8 deletions

View File

@ -33,7 +33,7 @@ module Crypto::Secret
extend ClassMethods extend ClassMethods
# For debugging. # For debugging. Leaks the secret
# #
# Returned String **not** tracked or wiped # Returned String **not** tracked or wiped
def hexstring : String def hexstring : String
@ -92,6 +92,7 @@ module Crypto::Secret
wipe wipe
end end
# Wipes data & makes this object available for reuse
def reset def reset
wipe wipe
end end
@ -132,13 +133,20 @@ module Crypto::Secret
end end
end end
# Marks a region allocated using as read & write depending on implementation. # Marks a region as read & write depending on implementation.
abstract def readwrite : self abstract def readwrite : self
# Marks a region allocated using as read-only depending on implementation. # Marks a region as read-only depending on implementation.
abstract def readonly : self abstract def readonly : self
# Makes a region allocated inaccessible depending on implementation. It cannot be read or written, but the data are preserved. # Makes a region inaccessible depending on implementation. It cannot be read or written, but the data are preserved.
abstract def noaccess : self abstract def noaccess : self
# Temporarily marks a region as read & write depending on implementation and yields `Bytes`
abstract def readwrite(& : Bytes -> U) forall U
# Temporarily marks a region as readonly depending on implementation and yields `Bytes`
abstract def readonly(& : Bytes -> U) forall U
# Temporarily Makes a region inaccessible depending on implementation. It cannot be read or written, but the data are preserved.
abstract def noaccess(& : Bytes -> U) forall U
protected abstract def to_slice(& : Bytes -> U) forall U protected abstract def to_slice(& : Bytes -> U) forall U
abstract def bytesize : Int32 abstract def bytesize : Int32

View File

@ -40,7 +40,7 @@ module Crypto::Secret
# Temporarily make buffer readonly within the block returning to the prior state on exit. # Temporarily make buffer readonly within the block returning to the prior state on exit.
# WARNING: Not thread safe unless this object is readonly or readwrite # WARNING: Not thread safe unless this object is readonly or readwrite
def readonly def readonly(& : Bytes -> U) forall U
with_state State::Readonly do with_state State::Readonly do
to_slice do |slice| to_slice do |slice|
yield slice yield slice

View File

@ -20,7 +20,7 @@ module Crypto::Secret::Stateless
# `slice` is only available within the block # `slice` is only available within the block
# #
# Not thread safe # Not thread safe
def readwrite def readwrite(& : Bytes -> U) forall U
to_slice do |slice| to_slice do |slice|
yield slice yield slice
end end
@ -35,7 +35,7 @@ module Crypto::Secret::Stateless
# Don't write to it # Don't write to it
# #
# Not thread safe # Not thread safe
def readonly def readonly(& : Bytes -> U) forall U
to_slice do |slice| to_slice do |slice|
yield slice yield slice
end end
@ -47,7 +47,7 @@ module Crypto::Secret::Stateless
end end
# Not thread safe # Not thread safe
def noaccess def noaccess(& : Bytes -> U) forall U
yield yield
end end