Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. import com.google.api.services.cloudkms.v1.CloudKMS;
  2. import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
  3. import org.bouncycastle.operator.ContentSigner;
  4. import org.bouncycastle.operator.DefaultSignatureAlgorithmIdentifierFinder;
  5.  
  6. import java.io.ByteArrayOutputStream;
  7. import java.io.IOException;
  8. import java.io.OutputStream;
  9. import java.security.NoSuchAlgorithmException;
  10.  
  11. import static sg.sleek.filesign.signature.GoogleKMSSimpleSign.createAuthorizedClient;
  12. import static sg.sleek.filesign.signature.GoogleKMSSimpleSign.signAsymmetric;
  13.  
  14. public class GoogleKMSContentSigner implements ContentSigner {
  15. private ByteArrayOutputStream outputStream;
  16. private AlgorithmIdentifier sigAlgId;
  17. private String keyPath;
  18. private String googleAuthorisationKeyFileName;
  19.  
  20. /**
  21. * Initialise Google KMS content signer
  22. * @param keyPath path to a KMS key
  23. * @param googleAuthorisationKeyFileName File name inside resource to access KMS
  24. */
  25. public GoogleKMSContentSigner(String keyPath, String googleAuthorisationKeyFileName){
  26. this.keyPath = keyPath;
  27. this.googleAuthorisationKeyFileName = googleAuthorisationKeyFileName;
  28. this.sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find("SHA256WITHRSAANDMGF1");
  29. this.outputStream = new ByteArrayOutputStream();
  30. }
  31.  
  32. @Override
  33. public AlgorithmIdentifier getAlgorithmIdentifier() {
  34. return this.sigAlgId;
  35. }
  36.  
  37. @Override
  38. public OutputStream getOutputStream() {
  39. return this.outputStream;
  40. }
  41.  
  42. @Override
  43. public byte[] getSignature() {
  44. try {
  45. CloudKMS kms = createAuthorizedClient(this.googleAuthorisationKeyFileName);
  46.  
  47. byte[] signedAttributeSet = outputStream.toByteArray();
  48.  
  49. return signAsymmetric(signedAttributeSet, kms, this.keyPath);
  50.  
  51. } catch (IOException | NoSuchAlgorithmException e) {
  52. e.printStackTrace();
  53. throw new RuntimeException("Unable to sign with KMS");
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement