radicle-crypto: ExtendedSignature constructor and destructor

Add `ExtendedSignature::new` to construct an `ExtendedSignature`
provided a public key and signature. The `namespace` chosen was
"radicle" and `reserved` is set to be empty -- these choices were
slightly arbitrary.

Also add a desctructor in the form of:
`From<ExtendedSignature> for (PublicKey, Signature)`

Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com>
X-Clacks-Overhead: GNU Terry Pratchett
This commit is contained in:
Fintan Halpenny 2022-11-02 14:29:37 +00:00
parent 3af5a7084d
commit 58c26e1c9d
No known key found for this signature in database
GPG Key ID: 2552FB6F64066CB7
1 changed files with 17 additions and 0 deletions

View File

@ -227,6 +227,12 @@ pub struct ExtendedSignature {
signature: crypto::Signature, signature: crypto::Signature,
} }
impl From<ExtendedSignature> for (crypto::PublicKey, crypto::Signature) {
fn from(ex: ExtendedSignature) -> Self {
(ex.public_key, ex.signature)
}
}
impl Encodable for ExtendedSignature { impl Encodable for ExtendedSignature {
type Error = ExtendedSignatureError; type Error = ExtendedSignatureError;
@ -263,6 +269,17 @@ impl ExtendedSignature {
const ARMORED_WIDTH: usize = 70; const ARMORED_WIDTH: usize = 70;
const MAGIC_PREAMBLE: &[u8] = b"SSHSIG"; const MAGIC_PREAMBLE: &[u8] = b"SSHSIG";
pub fn new(public_key: crypto::PublicKey, signature: crypto::Signature) -> Self {
Self {
version: 1,
public_key,
namespace: b"radicle".to_vec(),
reserved: b"".to_vec(),
hash_algorithm: b"sha256".to_vec(),
signature,
}
}
pub fn from_armored(s: &[u8]) -> Result<Self, ExtendedSignatureError> { pub fn from_armored(s: &[u8]) -> Result<Self, ExtendedSignatureError> {
let s = s let s = s
.strip_prefix(Self::ARMORED_HEADER) .strip_prefix(Self::ARMORED_HEADER)