List of usage examples for java.security Signature update
public final void update(ByteBuffer data) throws SignatureException
From source file:org.structr.util.StructrLicenseManager.java
private static void sign(final Map<String, String> properties, final String keystoreFileName, final String password) { final String src = collectLicenseFieldsForSignature(properties); try {//w w w.j a v a 2 s .com final byte[] data = src.getBytes(CharSet); final Signature signer = Signature.getInstance(SignatureAlgorithm); final KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); try (final InputStream is = new FileInputStream(keystoreFileName)) { keyStore.load(is, password.toCharArray()); final Key key = keyStore.getKey(KeystoreAlias, password.toCharArray()); signer.initSign((PrivateKey) key); signer.update(data); properties.put(SignatureKey, Hex.encodeHexString(signer.sign())); } } catch (Throwable t) { logger.warn("Unable to sign license.", t); } }
From source file:Decoder.java
private byte[] checkAndGetLicenseText(String licenseContent) throws Exception { byte[] licenseText; try {//from ww w. j a va 2 s . c o m byte[] decodedBytes = Base64.decodeBase64(licenseContent.getBytes()); ByteArrayInputStream in = new ByteArrayInputStream(decodedBytes); DataInputStream dIn = new DataInputStream(in); int textLength = dIn.readInt(); licenseText = new byte[textLength]; dIn.read(licenseText); byte[] hash = new byte[dIn.available()]; dIn.read(hash); try { Signature signature = Signature.getInstance("SHA1withDSA"); signature.initVerify(PUBLIC_KEY); signature.update(licenseText); if (!signature.verify(hash)) { throw new Exception("Failed to verify the license."); } } catch (InvalidKeyException e) { throw new Exception(e); } catch (SignatureException e) { throw new Exception(e); } catch (NoSuchAlgorithmException e) { throw new Exception(e); } } catch (IOException e) { throw new Exception(e); } return licenseText; }
From source file:com.vmware.o11n.plugin.crypto.service.CryptoRSAService.java
/** * Creates an RSA Signature/*from ww w. jav a2 s . co m*/ * * @param pemKey RSA Private Key * @param dataB64 Base64 encoded data to sign * @return Base64 encoded signature * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException * @throws IOException * @throws InvalidKeyException * @throws SignatureException */ public String sign(String pemKey, String dataB64) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException, InvalidKeyException, SignatureException { String signatureB64 = null; PrivateKey privateKey = null; Key key = null; try { key = CryptoUtil.getKey(pemKey); } catch (IOException e) { //try to fix key: key = CryptoUtil.getKey(CryptoUtil.fixPemString(pemKey)); } if (key instanceof PrivateKey) { privateKey = (PrivateKey) key; } else { throw new IllegalArgumentException("Invalid key object type: " + key.getClass().getName()); } Signature signer = Signature.getInstance(SIGNATURE_ALGORITHM); signer.initSign(privateKey); signer.update(Base64.decodeBase64(dataB64)); byte[] sigBytes = signer.sign(); signatureB64 = Base64.encodeBase64String(sigBytes); return signatureB64; }
From source file:org.gluu.oxpush2.u2f.v2.cert.KeyPairGeneratorImpl.java
@Override public byte[] sign(byte[] signedData, PrivateKey privateKey) throws U2FException { try {/*from w w w . jav a 2 s .c o m*/ Signature signature = Signature.getInstance("SHA256withECDSA"); signature.initSign(privateKey); signature.update(signedData); return signature.sign(); } catch (NoSuchAlgorithmException ex) { throw new U2FException("Error when signing", ex); } catch (SignatureException ex) { throw new U2FException("Error when signing", ex); } catch (InvalidKeyException ex) { throw new U2FException("Error when signing", ex); } }
From source file:mx.bigdata.cfdi.TFDv1.java
public int verify(Certificate cert) throws Exception { if (tfd == null) { return 601; //No contiene timbrado }//from w ww.j a va 2 s .c o m Base64 b64 = new Base64(); String sigStr = tfd.getSelloSAT(); byte[] signature = b64.decode(sigStr); byte[] bytes = getOriginalBytes(); Signature sig = Signature.getInstance("SHA1withRSA"); sig.initVerify(cert); sig.update(bytes); boolean verified = sig.verify(signature); return verified ? 600 : 602; //Sello del timbrado no valido }
From source file:mx.bigdata.sat.cfdi.TFDv11c33.java
public int verificar() throws Exception { if (tfd == null) { return 601; //No contiene timbrado }//from w w w . java 2s . c o m Base64 b64 = new Base64(); String sigStr = tfd.getSelloSAT(); byte[] signature = b64.decode(sigStr); byte[] bytes = getOriginalBytes(); Signature sig = Signature.getInstance("SHA256withRSA"); sig.initVerify(cert); sig.update(bytes); boolean verified = sig.verify(signature); return verified ? 600 : 602; //Sello del timbrado no valido }
From source file:test.be.fedict.eid.applet.PKCS11Test.java
@Test public void testPKCS1viaPKCS11() throws Exception { File tmpConfigFile = File.createTempFile("pkcs11-", "conf"); tmpConfigFile.deleteOnExit();// ww w . j a va2s.c om PrintWriter configWriter = new PrintWriter(new FileOutputStream(tmpConfigFile), true); configWriter.println("name=SmartCard"); configWriter.println("library=/usr/lib/libbeidpkcs11.so.0"); configWriter.println("slotListIndex=2"); SunPKCS11 provider = new SunPKCS11(tmpConfigFile.getAbsolutePath()); Security.addProvider(provider); KeyStore keyStore = KeyStore.getInstance("PKCS11", provider); keyStore.load(null, null); PrivateKeyEntry privateKeyEntry = (PrivateKeyEntry) keyStore.getEntry("Authentication", null); PrivateKey privateKey = privateKeyEntry.getPrivateKey(); Signature signature = Signature.getInstance("SHA1withRSA"); signature.initSign(privateKey); byte[] toBeSigned = "hello world".getBytes(); signature.update(toBeSigned); byte[] signatureValue = signature.sign(); X509Certificate certificate = (X509Certificate) privateKeyEntry.getCertificate(); RSAPublicKey publicKey = (RSAPublicKey) certificate.getPublicKey(); BigInteger signatureValueBigInteger = new BigInteger(signatureValue); BigInteger messageBigInteger = signatureValueBigInteger.modPow(publicKey.getPublicExponent(), publicKey.getModulus()); LOG.debug("original message: " + new String(Hex.encodeHex(messageBigInteger.toByteArray()))); // LOG.debug("ASN.1 signature: " + ASN1Dump.dumpAsString(obj) }
From source file:com.vmware.o11n.plugin.crypto.service.CryptoRSAService.java
/** * Verify a RSA Signature with a RSA Public Key * * @param pemKey RSA Key (Public or Private, Public will be derived from Private) * @param dataB64 Base64 encoded data the signature was created from * @param signatureB64 Base64 Encoded RSA Signature to verify * @return// www . ja v a2s.c om * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException * @throws IOException * @throws InvalidKeyException * @throws SignatureException */ public boolean verifySignature(String pemKey, String dataB64, String signatureB64) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException, InvalidKeyException, SignatureException { boolean valid = false; PublicKey publicKey = null; Key key = null; try { key = CryptoUtil.getKey(pemKey); //can be private or public } catch (IOException e) { //try to fix key: key = CryptoUtil.getKey(CryptoUtil.fixPemString(pemKey)); } if (key instanceof RSAPublicKey) { publicKey = (RSAPublicKey) key; } else if (key instanceof RSAPrivateCrtKey) { RSAPrivateCrtKey privateKey = (RSAPrivateCrtKey) key; publicKey = CryptoUtil.getPublicFromPrivate(privateKey); } else { throw new IllegalArgumentException("Unknown key object type: " + key.getClass().getName()); } Signature signer = Signature.getInstance(SIGNATURE_ALGORITHM); signer.initVerify(publicKey); signer.update(Base64.decodeBase64(dataB64)); valid = signer.verify(Base64.decodeBase64(signatureB64)); return valid; }
From source file:mx.bigdata.sat.cfd.CFDv2.java
String getSignature(PrivateKey key) throws Exception { byte[] bytes = getOriginalBytes(); byte[] signed; String alg = getDigestAlgorithm(); Signature sig = Signature.getInstance(alg); sig.initSign(key);/*from ww w .j ava2 s .c o m*/ sig.update(bytes); signed = sig.sign(); Base64 b64 = new Base64(-1); return b64.encodeToString(signed); }
From source file:org.gluu.com.ox_push2.u2f.v2.cert.KeyPairGeneratorImpl.java
@Override public byte[] sign(byte[] signedData, PrivateKey privateKey) throws U2FException { try {//from www.j a v a2 s . c om Signature signature = Signature.getInstance("SHA256withECDSA", bouncyCastleProvider); signature.initSign(privateKey); signature.update(signedData); return signature.sign(); } catch (NoSuchAlgorithmException ex) { throw new U2FException("Error when signing", ex); } catch (SignatureException ex) { throw new U2FException("Error when signing", ex); } catch (InvalidKeyException ex) { throw new U2FException("Error when signing", ex); } }