Java RSA generateRSASHA1Signature(PrivateKey privateKey, String data)

Here you can find the source of generateRSASHA1Signature(PrivateKey privateKey, String data)

Description

Computes RSA-SHA1 signature.

License

Apache License

Parameter

Parameter Description
data - the data to be signed.
key - the signing key.

Exception

Parameter Description

Return

The Base64-encoded RFC 2104-compliant HMAC signature.

Declaration

public static String generateRSASHA1Signature(PrivateKey privateKey,
        String data) throws GeneralSecurityException 

Method Source Code

//package com.java2s;
/**//from  www  .  ja v a  2 s . c om
 *  Copyright 2010-2011 Buhake Sindi

 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://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License 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.

 */

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;

import java.security.PrivateKey;
import java.security.Signature;

import sun.misc.BASE64Encoder;

public class Main {
    /**
     * Computes RSA-SHA1 signature.
     * 
     * @param data
     *            - the data to be signed.
     * @param key
     *            - the signing key.
     * @return The Base64-encoded RFC 2104-compliant HMAC signature.
     * @throws java.security.SignatureException
     *             when signature generation fails
     */
    public static String generateRSASHA1Signature(PrivateKey privateKey,
            String data) throws GeneralSecurityException {
        byte[] rsaSha1Data = null;

        try {
            Signature signature = Signature.getInstance("SHA1withRSA");
            signature.initSign(privateKey);
            signature.update(data.getBytes("UTF-8"));
            rsaSha1Data = signature.sign();
            return new BASE64Encoder().encode(rsaSha1Data);
        } catch (UnsupportedEncodingException e) {
            // TODO: handle exception
            throw new GeneralSecurityException(e);
        }
    }
}

Related

  1. generateRSAKeyPair(int bitsize)
  2. generateRsaKeyPair(int keysize)
  3. generateRSAKeypairAndKeystore(String fullyQualifiedDN, Date endDate, String keystoreLocation, String keyPairAlias, String keypairPassword, String keystorePassword)
  4. generateRsaPublicKey(BigInteger modulus, BigInteger publicExponent)
  5. generateRSAPublicKey(final BigInteger modulus, final BigInteger publicExponent)
  6. generateSaltedHMAC(String accessKey, String userSalt, Mac mac)
  7. getPemKeyStore(File file)