All Downloads are FREE. Search and download functionalities are using the official Maven repository.

bali.notary.handlers.PublicKeyHandler Maven / Gradle / Ivy

There is a newer version: 3.6
Show newest version
/************************************************************************
 * 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;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy