List of usage examples for java.security Signature initSign
public final void initSign(PrivateKey privateKey) throws InvalidKeyException
From source file:com.boubei.tss.modules.license.LicenseFactory.java
/** * ?license???//w ww. j av a 2s . c o m * @param license * @throws Exception */ public static synchronized void sign(License license) throws Exception { String privateKey = FileHelper.readFile(new File(PRIVATE_KEY_FILE)); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(EasyUtils.decodeHex(privateKey.trim())); PrivateKey privKey = keyFactory.generatePrivate(privKeySpec); Signature sig = Signature.getInstance(SIGN_ALGORITHM); sig.initSign(privKey); sig.update(license.getFingerprint()); license.signature = EasyUtils.encodeHex(sig.sign()); }
From source file:net.sf.zekr.common.util.CryptoUtils.java
public static byte[] sign(byte[] text, byte[] prvKeyBytes) throws GeneralSecurityException { PKCS8EncodedKeySpec prvSpec = new PKCS8EncodedKeySpec(prvKeyBytes); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); PrivateKey prvKey = keyFactory.generatePrivate(prvSpec); Signature sig = Signature.getInstance("SHA1withDSA"); sig.initSign(prvKey); sig.update(text);/*from www. j a v a 2s . co m*/ return sig.sign(); }
From source file:com.linkage.crm.csb.sign.CtSignature.java
/** * ./*from w ww .ja v a 2 s . c om*/ * * @param pwd String * @param alias String * @param priKeyFile * @return Signature */ public static Signature createSignatureForSign(String pwd, String alias, String priKeyFile) { try { logger.debug("keypath=============" + priKeyFile); KeyStore ks = KeyStore.getInstance("JKS"); FileInputStream ksfis = new FileInputStream(priKeyFile); BufferedInputStream ksbufin = new BufferedInputStream(ksfis); char[] kpass = pwd.toCharArray(); ks.load(ksbufin, kpass); PrivateKey priKey = (PrivateKey) ks.getKey(alias, kpass); Signature rsa = Signature.getInstance("SHA1withDSA"); rsa.initSign(priKey); return rsa; } catch (Exception ex) { logger.error("errors appeared while trying to signature", ex); return null; } }
From source file:com.linkage.crm.csb.sign.CtSignature.java
/** * @param originalText String //ww w . j a v a 2 s . c o m * @param pwd String * @param alias String * @param priKeyFile * @return String */ public static String signature(String originalText, String pwd, String alias, String priKeyFile) { try { KeyStore ks = KeyStore.getInstance("JKS"); FileInputStream ksfis = new FileInputStream(priKeyFile); BufferedInputStream ksbufin = new BufferedInputStream(ksfis); char[] kpass = pwd.toCharArray(); ks.load(ksbufin, kpass); PrivateKey priKey = (PrivateKey) ks.getKey(alias, kpass); Signature rsa = Signature.getInstance("SHA1withDSA"); rsa.initSign(priKey); rsa.update(originalText.getBytes()); byte[] signedText = rsa.sign(); return HexUtils.toHexString(signedText); } catch (Exception ex) { logger.error("errors appeared while trying to signature", ex); return null; } }
From source file:nl.arietimmerman.u2f.server.CryptoHelper.java
public static byte[] sign(PrivateKey privateKey, byte[] message) { try {/*from ww w. j a v a2s. c om*/ Signature signatureObject = Signature.getInstance("SHA256withECDSA", new BouncyCastleProvider()); signatureObject.initSign(privateKey); signatureObject.update(message); return signatureObject.sign(); } catch (InvalidKeyException | SignatureException | NoSuchAlgorithmException e) { e.printStackTrace(); return null; } }
From source file:ee.sk.hwcrypto.demo.signature.TestSigningData.java
private static byte[] encrypt(final String javaSignatureAlgorithm, final PrivateKey privateKey, final byte[] bytes) { try {/* w w w.j av a2s . c o m*/ java.security.Signature signature = java.security.Signature.getInstance(javaSignatureAlgorithm); signature.initSign(privateKey); signature.update(bytes); final byte[] signatureValue = signature.sign(); return signatureValue; } catch (GeneralSecurityException e) { throw new RuntimeException(e); } }
From source file:hh.learnj.test.license.test.rsa.RSATest.java
/** * ???/*from w ww . j av a 2 s .c o m*/ * * @param target * @return * @throws Exception */ static String signByPrivateKey(String target) throws Exception { PrivateKey privateKey = getPrivateKey(); Signature signature = Signature.getInstance(ALGORITHM_SIGN); signature.initSign(privateKey); signature.update(target.getBytes("UTF-8")); String sign = encodeBase64(signature.sign()); System.out.println("???\r\n" + sign); return sign; }
From source file:org.dasein.cloud.google.GenerateToken.java
public static String getToken(String iss, String p12File) { String header = "{\"alg\":\"RS256\",\"typ\":\"JWT\"}"; String claimTemplate = "'{'\"iss\": \"{0}\", \"scope\": \"{1}\", \"aud\": \"{2}\", \"exp\": \"{3}\", \"iat\": \"{4}\"'}'"; try {//from w w w . j ava 2 s . co m StringBuffer token = new StringBuffer(); //Encode the JWT Header and add it to our string to sign token.append(Base64.encodeBase64URLSafeString(header.getBytes("UTF-8"))); //Separate with a period token.append("."); //Create the JWT Claims Object String[] claimArray = new String[5]; claimArray[0] = iss; claimArray[1] = "https://www.googleapis.com/auth/compute"; claimArray[2] = "https://accounts.google.com/o/oauth2/token"; claimArray[3] = Long.toString((System.currentTimeMillis() / 1000) + 300); claimArray[4] = Long.toString((System.currentTimeMillis() / 1000)); MessageFormat claims; claims = new MessageFormat(claimTemplate); String payload = claims.format(claimArray); // System.out.println(claimArray[3]); // System.out.println(claimArray[4]); //Add the encoded claims object token.append(Base64.encodeBase64URLSafeString(payload.getBytes("UTF-8"))); char[] password = "notasecret".toCharArray(); FileInputStream fin = new FileInputStream(new File(p12File)); KeyStore store = KeyStore.getInstance("PKCS12"); try { store.load(fin, password); } finally { try { fin.close(); } catch (IOException e) { } } String alias = ""; // KeyStore keystore = getKeyStore(password); Enumeration<String> enum1 = store.aliases(); // List the aliases while (enum1.hasMoreElements()) { String keyStoreAlias = enum1.nextElement().toString(); if (store.isKeyEntry(keyStoreAlias)) { //Does alias refer to a private key? alias = keyStoreAlias; break; } } PrivateKey privateKey = (PrivateKey) store.getKey(alias, password); //Sign the JWT Header + "." + JWT Claims Object Signature signature = Signature.getInstance("SHA256withRSA"); signature.initSign(privateKey); signature.update(token.toString().getBytes("UTF-8")); String signedPayload = Base64.encodeBase64URLSafeString(signature.sign()); //Separate with a period token.append("."); //Add the encoded signature token.append(signedPayload); // System.out.println(token.toString()); return token.toString(); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:org.oscarehr.common.hl7.v2.oscar_to_oscar.SendingUtils.java
private static byte[] getSignature(byte[] bytes, PrivateKey receiverServiceKey) throws SignatureException, InvalidKeyException, NoSuchAlgorithmException { Signature signature = Signature.getInstance("MD5WithRSA"); signature.initSign(receiverServiceKey); signature.update(bytes);// w w w.j ava2s . c om return (signature.sign()); }
From source file:org.syphr.utils.x509.X509Utils.java
/** * Create a signature using the given token and private key. * * @param message/*from ww w .j ava2 s.c o m*/ * the message to sign * @param key * the private key to use to create the signature (this must be * PKCS8 encoded) * @param keyAlg * the algorithm used to create the private key * @param sigAlg * the algorithm to use to create the signature * @return the signature * @throws IOException * if there is an error reading the key * @throws InvalidKeySpecException * if the key algorithm is not appropriate for the given private * key * @throws InvalidKeyException * if the given private key is not valid * @throws SignatureException * if there is an error while generating the signature */ public static byte[] sign(String message, InputStream key, KeyAlgorithm keyAlg, SignatureAlgorithm sigAlg) throws IOException, InvalidKeySpecException, InvalidKeyException, SignatureException { KeySpec privateKeySpec = new PKCS8EncodedKeySpec(IOUtils.toByteArray(key)); try { KeyFactory keyFactory = KeyFactory.getInstance(keyAlg.getAlgorithm()); PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec); Signature sig = Signature.getInstance(sigAlg.getAlgorithm()); sig.initSign(privateKey); sig.update(message.getBytes()); return sig.sign(); } catch (NoSuchAlgorithmException e) { /* * This is protected against by enforcing specific algorithm * choices. */ throw new IllegalArgumentException("Unknown algorithm", e); } }