Last active
          December 11, 2015 11:38 
        
      - 
      
- 
        Save anoopsankar/4594587 to your computer and use it in GitHub Desktop. 
    PKCS#1 digital signature
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | private byte[] sign(byte[] data, PrivateKey privateKey) throws GeneralSecurityException { | |
| Security.addProvider(new BouncyCastleProvider()); | |
| Signature signer = Signature.getInstance("SHA1withRSA", "BC"); | |
| signer.initSign(privateKey); | |
| signer.update(data); | |
| return signer.sign(); | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public byte[] signWithExternDigest(byte[] data, PrivateKey privateKey) throws GeneralSecurityException, IOException { | |
| AlgorithmIdentifier sha1AlgoId = new AlgorithmIdentifier(new ASN1ObjectIdentifier("1.3.14.3.2.26")); | |
| DigestInfo digestInfo = new DigestInfo(sha1AlgoId, data); | |
| byte[] paddedDigest = digestInfo.getEncoded("DER"); | |
| Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC"); | |
| cipher.init(Cipher.ENCRYPT_MODE, privateKey); | |
| return cipher.doFinal(paddedDigest); | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment