Sodium::SecretBox::PublicKey add JSON and Yaml converters.

Sodium::Sign::PublicKey add JSON and Yaml converters.
master
Didactic Drunk 2019-08-29 01:16:48 -07:00
parent 740e49cc6b
commit a825d61039
2 changed files with 38 additions and 0 deletions

View File

@ -33,5 +33,24 @@ class Sodium::CryptoBox
end end
dst dst
end end
module SerializeConverter
def self.to_json(value : PublicKey, json : JSON::Builder)
json.string Base64.strict_encode(value.to_slice)
end
def self.from_json(value : JSON::PullParser) : PublicKey
PublicKey.new Base64.decode(value.read_string)
end
def self.to_yaml(value : PublicKey, yaml : YAML::Nodes::Builder)
yaml.scalar Base64.strict_encode(value.to_slice)
end
def self.from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node) : PublicKey
node.raise "Expected scalar, not #{node.class}" unless node.is_a?(YAML::Nodes::Scalar)
PublicKey.new Base64.decode(node.value)
end
end
end end
end end

View File

@ -34,5 +34,24 @@ module Sodium
raise Sodium::Error::VerificationFailed.new("crypto_sign_verify_detached") raise Sodium::Error::VerificationFailed.new("crypto_sign_verify_detached")
end end
end end
module SerializeConverter
def self.to_json(value : PublicKey, json : JSON::Builder)
json.string Base64.strict_encode(value.to_slice)
end
def self.from_json(value : JSON::PullParser) : PublicKey
PublicKey.new Base64.decode(value.read_string)
end
def self.to_yaml(value : PublicKey, yaml : YAML::Nodes::Builder)
yaml.scalar Base64.strict_encode(value.to_slice)
end
def self.from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node) : PublicKey
node.raise "Expected scalar, not #{node.class}" unless node.is_a?(YAML::Nodes::Scalar)
PublicKey.new Base64.decode(node.value)
end
end
end end
end end