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

craterdog.notary.mappers.PrivateKeyDeserializer Maven / Gradle / Ivy

/************************************************************************
 * 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 craterdog.notary.mappers;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import craterdog.security.MessageCryptex;
import craterdog.security.RsaAesMessageCryptex;
import java.io.IOException;
import java.security.PrivateKey;

/**
 * This class handles the unmarshaling of a private key from an encrypted PEM string.
 *
 * @author Derk Norton
 */
public class PrivateKeyDeserializer extends JsonDeserializer {

    static private final MessageCryptex cryptex = new RsaAesMessageCryptex();

    private final char[] password;


    public PrivateKeyDeserializer() {
        this.password = null;
    }


    public PrivateKeyDeserializer(char[] password) {
        this.password = password;
    }


    @Override
    public PrivateKey deserialize(JsonParser p, DeserializationContext ctxt)
            throws IOException, JsonProcessingException {
        PrivateKey privateKey = null;
        if (password != null) {
            privateKey = cryptex.decodePrivateKey(p.getValueAsString(), password);
        }
        return privateKey;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy