From a825d610396cd7145aa58870d79e05326891e1a6 Mon Sep 17 00:00:00 2001 From: Didactic Drunk <1479616+didactic-drunk@users.noreply.github.com> Date: Thu, 29 Aug 2019 01:16:48 -0700 Subject: [PATCH] Sodium::SecretBox::PublicKey add JSON and Yaml converters. Sodium::Sign::PublicKey add JSON and Yaml converters. --- src/sodium/crypto_box/public_key.cr | 19 +++++++++++++++++++ src/sodium/sign/public_key.cr | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/sodium/crypto_box/public_key.cr b/src/sodium/crypto_box/public_key.cr index 9029a67..0736122 100644 --- a/src/sodium/crypto_box/public_key.cr +++ b/src/sodium/crypto_box/public_key.cr @@ -33,5 +33,24 @@ class Sodium::CryptoBox end dst 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 diff --git a/src/sodium/sign/public_key.cr b/src/sodium/sign/public_key.cr index a0dc20f..f4eaedd 100644 --- a/src/sodium/sign/public_key.cr +++ b/src/sodium/sign/public_key.cr @@ -34,5 +34,24 @@ module Sodium raise Sodium::Error::VerificationFailed.new("crypto_sign_verify_detached") 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