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

com.amazonaws.mobileconnectors.iot.PrivateKeyReader Maven / Gradle / Ivy

Go to download

The AWS Android SDK for AWS IoT module holds the client classes that are used for communicating with AWS IoT Service

There is a newer version: 2.6.27
Show newest version
/**
 * Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *    http://aws.amazon.com/apache2.0
 *
 * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
 * OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and
 * limitations under the License.
 */

package com.amazonaws.mobileconnectors.iot;

import com.amazonaws.util.StringUtils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.PrivateKey;
import java.security.spec.InvalidKeySpecException;

/**
 * Class for reading RSA private key from PEM string. It can read PEM files with
 * PKCS#8 or PKCS#1 encodings. It does not support encrypted PEM files.
 */
class PrivateKeyReader {

    /**
     * PEM string to be parsed for a key.
     */
    private final String keyPemString;

    /**
     * Create a PEM private key reader.
     *
     * @param keyPemString The private key in PEM format.
     */
    public PrivateKeyReader(String keyPemString) {
        this.keyPemString = keyPemString;
    }

    /**
     * Get a Private Key from the string.
     *
     * @return Private key
     * @throws IOException if error reading PEM string.
     * @throws InvalidKeySpecException if PKCS#1 key spec unavailable.
     */
    public PrivateKey getPrivateKey() throws IOException, InvalidKeySpecException {

        InputStream keyInputStream = new ByteArrayInputStream(
                keyPemString.getBytes(StringUtils.UTF8));
        return PEM.readPrivateKey(keyInputStream);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy