bali.notary.handlers.PublicKeyHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-bali-digital-notary Show documentation
Show all versions of java-bali-digital-notary Show documentation
This project defines the Java interfaces and classes for a digital notary.
/************************************************************************
* Copyright (c) Crater Dog Technologies(TM). All Rights Reserved. *
************************************************************************
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. *
* *
* This code is free software; you can redistribute it and/or modify it *
* under the terms of The MIT License (MIT), as published by the Open *
* Source Initiative. (See http://opensource.org/licenses/MIT) *
************************************************************************/
package bali.notary.handlers;
import bali.language.handlers.JavaTypeHandler;
import bali.language.nodes.ComponentNode;
import bali.language.nodes.TextNode;
import craterdog.security.MessageCryptex;
import craterdog.security.RsaAesMessageCryptex;
import java.security.PublicKey;
/**
* This class handles the marshaling of a public key into a PEM string.
*
* @author Derk Norton
*/
public final class PublicKeyHandler implements JavaTypeHandler {
static private final MessageCryptex cryptex = new RsaAesMessageCryptex();
@Override
public ComponentNode fromObject(T publicKey, Class>... parameterTypes) {
String pemValue = cryptex.encodePublicKey(publicKey);
TextNode textNode = new TextNode("\"\n" + pemValue + "\n\"");
ComponentNode componentNode = new ComponentNode(textNode);
return componentNode;
}
@Override
public PublicKey toObject(ComponentNode componentNode, Class> targetType, Class>... parameterTypes) {
TextNode textNode = (TextNode) componentNode.literal;
String pemValue = textNode.toRawString();
PublicKey publicKey = cryptex.decodePublicKey(pemValue);
return publicKey;
}
}