sign File by key - Java Security

Java examples for Security:Key

Description

sign File by key

Demo Code


//package com.java2s;

import java.io.File;
import java.io.FileInputStream;

import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.KeyStore;
import java.security.PrivateKey;

import java.security.Signature;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

public class Main {


    public static final String KEY_STORE = "JKS";
    public static final String X509 = "X.509";

    @Deprecated//from www.  ja  v  a  2 s.  c  o m
    public static byte[] signFile(String filePath, String keyStorePath,
            String alias, String password) throws Exception {
        byte[] sign = new byte[0];

        X509Certificate x509Certificate = (X509Certificate) getCertificate(
                keyStorePath, alias, password);

        KeyStore keyStore = getKeyStore(keyStorePath, password);

        PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias,
                password.toCharArray());

        Signature signature = Signature.getInstance(x509Certificate
                .getSigAlgName());
        signature.initSign(privateKey);
        File file = new File(filePath);
        if (file.exists()) {
            FileInputStream in = new FileInputStream(file);
            FileChannel fileChannel = in.getChannel();
            MappedByteBuffer byteBuffer = fileChannel.map(
                    FileChannel.MapMode.READ_ONLY, 0, file.length());
            signature.update(byteBuffer);
            fileChannel.close();
            in.close();
            sign = signature.sign();
        }
        return sign;
    }

    private static Certificate getCertificate(String certificatePath)
            throws Exception {
        CertificateFactory certificateFactory = CertificateFactory
                .getInstance(X509);
        FileInputStream in = new FileInputStream(certificatePath);
        Certificate certificate = certificateFactory
                .generateCertificate(in);
        in.close();
        return certificate;
    }

    private static Certificate getCertificate(String keyStorePath,
            String alias, String password) throws Exception {
        KeyStore keyStore = getKeyStore(keyStorePath, password);
        Certificate certificate = keyStore.getCertificate(alias);
        return certificate;
    }

    private static KeyStore getKeyStore(String keyStorePath, String password)
            throws Exception {
        FileInputStream in = new FileInputStream(keyStorePath);
        KeyStore keyStore = KeyStore.getInstance(KEY_STORE);
        keyStore.load(in, password.toCharArray());
        in.close();
        return keyStore;
    }

    public static byte[] sign(byte[] data, String keyStorePath,
            String alias, String password) throws Exception {
        // ?
        X509Certificate x509Certificate = (X509Certificate) getCertificate(
                keyStorePath, alias, password);
        // ?
        KeyStore keyStore = getKeyStore(keyStorePath, password);
        // ?
        PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias,
                password.toCharArray());
        // 
        Signature signature = Signature.getInstance(x509Certificate
                .getSigAlgName());
        signature.initSign(privateKey);
        signature.update(data);
        return signature.sign();
    }
}

Related Tutorials