List of usage examples for java.security Signature update
public final void update(byte[] data, int off, int len) throws SignatureException
From source file:Main.java
public static void main(String[] argv) throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); keyGen.initialize(1024);/*from w w w .ja v a 2s .co m*/ KeyPair keypair = keyGen.genKeyPair(); DSAPrivateKey privateKey = (DSAPrivateKey) keypair.getPrivate(); DSAPublicKey publicKey = (DSAPublicKey) keypair.getPublic(); DSAParams dsaParams = privateKey.getParams(); BigInteger p = dsaParams.getP(); BigInteger q = dsaParams.getQ(); BigInteger g = dsaParams.getG(); BigInteger x = privateKey.getX(); BigInteger y = publicKey.getY(); // Create the DSA key factory KeyFactory keyFactory = KeyFactory.getInstance("DSA"); // Create the DSA private key KeySpec privateKeySpec = new DSAPrivateKeySpec(x, p, q, g); PrivateKey privateKey1 = keyFactory.generatePrivate(privateKeySpec); byte[] buffer = new byte[1024]; Signature sig = Signature.getInstance(privateKey1.getAlgorithm()); sig.initSign(privateKey1); sig.update(buffer, 0, buffer.length); }
From source file:Main.java
public static void main(String[] argv) throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); keyGen.initialize(1024);/*from w w w . j a v a2 s.co m*/ KeyPair keypair = keyGen.genKeyPair(); DSAPrivateKey privateKey = (DSAPrivateKey) keypair.getPrivate(); DSAPublicKey publicKey = (DSAPublicKey) keypair.getPublic(); DSAParams dsaParams = privateKey.getParams(); BigInteger p = dsaParams.getP(); BigInteger q = dsaParams.getQ(); BigInteger g = dsaParams.getG(); BigInteger x = privateKey.getX(); BigInteger y = publicKey.getY(); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); KeySpec publicKeySpec = new DSAPublicKeySpec(y, p, q, g); PublicKey publicKey1 = keyFactory.generatePublic(publicKeySpec); KeySpec privateKeySpec = new DSAPrivateKeySpec(x, p, q, g); PrivateKey privateKey1 = keyFactory.generatePrivate(privateKeySpec); byte[] buffer = new byte[1024]; Signature sig = Signature.getInstance(privateKey1.getAlgorithm()); sig.initSign(privateKey1); sig.update(buffer, 0, buffer.length); byte[] signature = sig.sign(); sig = Signature.getInstance(publicKey1.getAlgorithm()); sig.initVerify(publicKey1); sig.update(buffer, 0, buffer.length); sig.verify(signature); }
From source file:GenSig.java
public static void main(String[] args) { /* Generate a DSA signature */ if (args.length != 1) { System.out.println("Usage: GenSig nameOfFileToSign"); } else/*w w w . j av a 2 s .c om*/ try { /* Generate a key pair */ KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA", "SUN"); SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN"); keyGen.initialize(1024, random); KeyPair pair = keyGen.generateKeyPair(); PrivateKey priv = pair.getPrivate(); PublicKey pub = pair.getPublic(); /* * Create a Signature object and initialize it with the private * key */ Signature dsa = Signature.getInstance("SHA1withDSA", "SUN"); dsa.initSign(priv); /* Update and sign the data */ FileInputStream fis = new FileInputStream(args[0]); BufferedInputStream bufin = new BufferedInputStream(fis); byte[] buffer = new byte[1024]; int len; while (bufin.available() != 0) { len = bufin.read(buffer); dsa.update(buffer, 0, len); } ; bufin.close(); /* * Now that all the data to be signed has been read in, generate * a signature for it */ byte[] realSig = dsa.sign(); /* Save the signature in a file */ FileOutputStream sigfos = new FileOutputStream("sig"); sigfos.write(realSig); sigfos.close(); /* Save the public key in a file */ byte[] key = pub.getEncoded(); FileOutputStream keyfos = new FileOutputStream("suepk"); keyfos.write(key); keyfos.close(); } catch (Exception e) { System.err.println("Caught exception " + e.toString()); } }
From source file:net.sf.zekr.common.util.CryptoUtils.java
public static byte[] sign(String datafile, PrivateKey prvKey, String sigAlg) throws Exception { Signature sig = Signature.getInstance(sigAlg); sig.initSign(prvKey);//from ww w. j av a2 s .c o m FileInputStream fis = new FileInputStream(datafile); byte[] dataBytes = new byte[1024]; int nread = fis.read(dataBytes); while (nread > 0) { sig.update(dataBytes, 0, nread); nread = fis.read(dataBytes); } return sig.sign(); }
From source file:com.threerings.getdown.tools.Digester.java
/** * Creates a digest file in the specified application directory. */// ww w. j a va2s . c o m public static void signDigest(File appdir, File storePath, String storePass, String storeAlias) throws IOException, GeneralSecurityException { File inputFile = new File(appdir, Digest.DIGEST_FILE); File signatureFile = new File(appdir, Digest.DIGEST_FILE + Application.SIGNATURE_SUFFIX); FileInputStream storeInput = null, dataInput = null; FileOutputStream signatureOutput = null; try { // initialize the keystore KeyStore store = KeyStore.getInstance("JKS"); storeInput = new FileInputStream(storePath); store.load(storeInput, storePass.toCharArray()); PrivateKey key = (PrivateKey) store.getKey(storeAlias, storePass.toCharArray()); // sign the digest file Signature sig = Signature.getInstance("SHA1withRSA"); dataInput = new FileInputStream(inputFile); byte[] buffer = new byte[8192]; int length; sig.initSign(key); while ((length = dataInput.read(buffer)) != -1) { sig.update(buffer, 0, length); } // Write out the signature signatureOutput = new FileOutputStream(signatureFile); String signed = new String(Base64.encodeBase64(sig.sign())); signatureOutput.write(signed.getBytes("utf8")); } finally { StreamUtil.close(signatureOutput); StreamUtil.close(dataInput); StreamUtil.close(storeInput); } }
From source file:oscar.oscarLab.ca.all.pageUtil.LabUploadAction.java
public static boolean validateSignature(PublicKey key, String sigString, File input) { Base64 base64 = new Base64(0); byte[] buf = new byte[1024]; try {//w ww. j a v a2 s . co m InputStream msgIs = new FileInputStream(input); Signature sig = Signature.getInstance("MD5WithRSA"); sig.initVerify(key); // Read in the message bytes and update the signature int numRead = 0; while ((numRead = msgIs.read(buf)) >= 0) { sig.update(buf, 0, numRead); } msgIs.close(); return (sig.verify(base64.decode(sigString.getBytes(MiscUtils.ENCODING)))); } catch (Exception e) { logger.debug("Could not validate signature: " + e); MiscUtils.getLogger().error("Error", e); return (false); } }
From source file:net.sf.keystore_explorer.crypto.signing.MidletSigner.java
private static byte[] signJarDigest(File jarFile, RSAPrivateKey privateKey) throws CryptoException { // Create a SHA-1 signature for the supplied JAR file FileInputStream fis = null;/*from w w w. j a v a 2 s . c o m*/ try { Signature signature = Signature.getInstance(SignatureType.SHA1_RSA.jce()); signature.initSign(privateKey); fis = new FileInputStream(jarFile); byte buffer[] = new byte[1024]; int read = 0; while ((read = fis.read(buffer)) != -1) { signature.update(buffer, 0, read); } return signature.sign(); } catch (IOException ex) { throw new CryptoException(res.getString("JarDigestSignatureFailed.exception.message"), ex); } catch (GeneralSecurityException ex) { throw new CryptoException(res.getString("JarDigestSignatureFailed.exception.message"), ex); } finally { IOUtils.closeQuietly(fis); } }
From source file:Main.java
/** * Verify a signature of a stream.//from www. ja v a 2 s. com * * @param cert The certificate containing the public key which will be used * to verify the signature. * @param signature The signature to verify. * @param stream The stream to verify. * @return boolean true if the signature was valid otherwise false. */ public static boolean verifySignature(String algorithm, Certificate cert, byte[] signature, InputStream stream) throws InvalidKeyException, SignatureException, IOException { Signature sign; try { sign = Signature.getInstance(algorithm); } catch (NoSuchAlgorithmException badsigner) { throw new IOException("Could not initialize signer with algorithm " + algorithm); } sign.initVerify(cert); byte[] buffer = new byte[1024]; while (true) { int read = stream.read(buffer); if (read < 0) { break; } sign.update(buffer, 0, read); } return sign.verify(signature); }
From source file:jef.tools.security.EncrypterUtil.java
/** * DSA??/* w w w . j a va 2 s . c o m*/ * * @param in * @return * @throws InvalidKeyException * @throws NoSuchAlgorithmException * @throws IOException * @throws SignatureException */ public static byte[] getDSASign(InputStream in, PrivateKey key) { try { java.security.Signature signet = java.security.Signature.getInstance("DSA"); signet.initSign(key); byte[] b = new byte[1024]; int len; while ((len = in.read(b)) != -1) { signet.update(b, 0, len); } byte[] signed = signet.sign(); return signed; } catch (IOException e) { throw new RuntimeException(e); } catch (GeneralSecurityException e) { throw new RuntimeException(e); } }
From source file:jef.tools.security.EncrypterUtil.java
/** * ????/* ww w .j a v a2 s.c o m*/ * * @param in * @param key * @param sign * @return */ public static boolean verifyDSASign(InputStream in, byte[] sign, PublicKey key) { Signature signetcheck; try { signetcheck = java.security.Signature.getInstance("DSA"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } try { signetcheck.initVerify(key); byte[] b = new byte[1024]; int len; while ((len = in.read(b)) != -1) { signetcheck.update(b, 0, len); } return signetcheck.verify(sign); } catch (InvalidKeyException e) { throw new RuntimeException(e); } catch (SignatureException e) { return false; } catch (IOException e) { throw new RuntimeException(e); } }