本文整理匯總了Java中java.security.Signature.initSign方法的典型用法代碼示例。如果您正苦於以下問題:Java Signature.initSign方法的具體用法?Java Signature.initSign怎麽用?Java Signature.initSign使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.security.Signature
的用法示例。
在下文中一共展示了Signature.initSign方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: signatureTest
import java.security.Signature; //導入方法依賴的package包/類
private void signatureTest(ValueSigner signer, KeyPairGenerator generator) throws Exception {
byte[] data = "HELLO".getBytes();
Signature signature = signer.getInstance(null);
KeyPair pair = generator.generateKeyPair();
signature.initSign(pair.getPrivate());
signature.update(data);
byte[] digest = signature.sign();
assertThat("digest has a value", TestUtils.wrap(digest), is(not(emptyArray())));
signature.initVerify(pair.getPublic());
signature.update(data);
assertThat("verification succeeds", signature.verify(digest), is(true));
switch (signer) {
case RSA_PKCS1:
break;
default:
signature.initSign(pair.getPrivate());
signature.update(data);
assertThat("signatures correctly use random padding", signature.sign(), not(equalTo(digest)));
}
}
示例2: sign
import java.security.Signature; //導入方法依賴的package包/類
/**
* 簽名
*
* @param data 待簽名數據
* @param privateKey 私鑰
* @return byte[] 數字簽名
* @throws Exception
*/
public static byte[] sign(byte[] data, byte[] privateKey) throws Exception {
// 轉換私鑰材料
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKey);
// 實例化密鑰工廠
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
// 取私鑰匙對象
PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);
// 實例化Signature
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
// 初始化Signature
signature.initSign(priKey);
// 更新
signature.update(data);
// 簽名
return signature.sign();
}
示例3: sign
import java.security.Signature; //導入方法依賴的package包/類
/**
* 用私鑰對信息生成數字簽名
*
* @param data
* 加密數據
* @param privateKey
* 私鑰
*
* @return
* @throws Exception
*/
public static String sign(byte[] data, String privateKey) throws Exception {
// 解密由base64編碼的私鑰
byte[] keyBytes = decryptBASE64(privateKey);
// 構造PKCS8EncodedKeySpec對象
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
// KEY_ALGORITHM 指定的加密算法
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
// 取私鑰匙對象
PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);
// 用私鑰對信息生成數字簽名
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initSign(priKey);
signature.update(data);
return encryptBASE64(signature.sign());
}
示例4: createEncoder
import java.security.Signature; //導入方法依賴的package包/類
@Override
public OneWayCodec createEncoder() throws Exception {
final Signature signature = Signature.getInstance(signatureAlgorithmName);
signature.initSign(keyPair.getPrivate());
return new OneWayCodec() {
@Override
public byte[] code(final byte[] data) throws Exception {
final int dataLen = data.length;
final byte[] b = new byte[dataLen + signatureLength];
System.arraycopy(data, 0, b, 0, dataLen);
signature.update(data);
signature.sign(b, dataLen, signatureLength);
return b;
}
};
}
示例5: sign
import java.security.Signature; //導入方法依賴的package包/類
private byte[] sign(byte[] message) throws GeneralSecurityException {
if (privateKey == null) {
throw new IllegalStateException("need to set private key with " +
"OAuthConsumer.setProperty when " +
"generating RSA-SHA1 signatures.");
}
Signature signer = Signature.getInstance("SHA1withRSA");
signer.initSign(privateKey);
signer.update(message);
return signer.sign();
}
示例6: use
import java.security.Signature; //導入方法依賴的package包/類
static boolean use(KeyPair kp) throws Exception {
Signature sig = Signature.getInstance("SHA1withDSA");
sig.initSign(kp.getPrivate());
sig.update(data);
byte[] signed = sig.sign();
Signature sig2 = Signature.getInstance("SHA1withDSA");
sig2.initVerify(kp.getPublic());
sig2.update(data);
return sig2.verify(signed);
}
示例7: sign
import java.security.Signature; //導入方法依賴的package包/類
/**
* 用私鑰對信息生成數字簽名
*
* @param data 加密數據
* @param privateKey 私鑰
* @return
* @throws Exception
*/
public static String sign(byte[] data, String privateKey) throws Exception {
byte[] keyBytes = BASE64.decode(privateKey); // 解密由base64編碼的私鑰
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); // 構造PKCS8EncodedKeySpec對象
KeyFactory keyFactory = KeyFactory.getInstance(Algorithm.RSA.getType()); // KEY_ALGORITHM 指定的加密算法
PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec); // 取私鑰匙對象
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); // 用私鑰對信息生成數字簽名
signature.initSign(priKey);
signature.update(data);
return BASE64.encodeToString(signature.sign());
}
示例8: signBySoft256
import java.security.Signature; //導入方法依賴的package包/類
/**
* @param privateKey
* @param data
* @return
* @throws Exception
*/
public static byte[] signBySoft256(PrivateKey privateKey, byte[] data)
throws Exception {
byte[] result = null;
Signature st = Signature.getInstance(BC_PROV_ALGORITHM_SHA256RSA, "BC");
st.initSign(privateKey);
st.update(data);
result = st.sign();
return result;
}
示例9: signBySoft
import java.security.Signature; //導入方法依賴的package包/類
/**
* @param privateKey
* @param data
* @return
* @throws Exception
*/
public static byte[] signBySoft(PrivateKey privateKey, byte[] data)
throws Exception {
byte[] result = null;
Signature st = Signature.getInstance(BC_PROV_ALGORITHM_SHA1RSA, "BC");
st.initSign(privateKey);
st.update(data);
result = st.sign();
return result;
}
示例10: sign
import java.security.Signature; //導入方法依賴的package包/類
/**
* 獲取數字簽名
*
* @param data 二進製位
* @param privateKey 私鑰(BASE64編碼)
* @return 數字簽名結果字符串
* @throws Exception 異常
*/
public static String sign(byte[] data, String privateKey) throws Exception {
byte[] keyBytes = Base64.decode(privateKey.getBytes(), Base64.DEFAULT);
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = getKeyFactory();
PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec);
Signature signature = Signature.getInstance("MD5withRSA");
signature.initSign(privateK);
signature.update(data);
return new String(Base64.encode(signature.sign(), Base64.DEFAULT));
}
示例11: calculateSignature
import java.security.Signature; //導入方法依賴的package包/類
static byte[] calculateSignature(
DERObjectIdentifier sigOid,
String sigName,
PrivateKey key,
SecureRandom random,
ASN1Encodable object)
throws IOException, NoSuchAlgorithmException, InvalidKeyException, SignatureException
{
Signature sig;
if (sigOid == null)
{
throw new IllegalStateException("no signature algorithm specified");
}
sig = X509Util.getSignatureInstance(sigName);
if (random != null)
{
sig.initSign(key, random);
}
else
{
sig.initSign(key);
}
sig.update(object.toASN1Primitive().getEncoded(ASN1Encoding.DER));
return sig.sign();
}
示例12: sign
import java.security.Signature; //導入方法依賴的package包/類
protected static byte[] sign(Class signatureClass, Object entity, Timestamp now, int depth, StringBuilder comment) throws Exception {
Signature signature = getSignature();
signature.initSign(CoreUtil.getUserContext().getPrivateKey());
updateSignature(entity, getEntitySignatures(signatureClass, entity.getClass(), depth), signature, CoreUtil.getUser(), now, comment);
//FileUtils.writeStringToFile(new java.io.File("d:\\ctsms\\sign_" + now.getTime() + ".txt"),comment.toString());
return signature.sign();
}
示例13: sign
import java.security.Signature; //導入方法依賴的package包/類
/**
* Encodes an X.509 CRL, and signs it using the given key.
*
* @param key the private key used for signing.
* @param algorithm the name of the signature algorithm used.
* @param provider the name of the provider.
*
* @exception NoSuchAlgorithmException on unsupported signature
* algorithms.
* @exception InvalidKeyException on incorrect key.
* @exception NoSuchProviderException on incorrect provider.
* @exception SignatureException on signature errors.
* @exception CRLException if any mandatory data was omitted.
*/
public void sign(PrivateKey key, String algorithm, String provider)
throws CRLException, NoSuchAlgorithmException, InvalidKeyException,
NoSuchProviderException, SignatureException {
try {
if (readOnly)
throw new CRLException("cannot over-write existing CRL");
Signature sigEngine = null;
if ((provider == null) || (provider.length() == 0))
sigEngine = Signature.getInstance(algorithm);
else
sigEngine = Signature.getInstance(algorithm, provider);
sigEngine.initSign(key);
// in case the name is reset
sigAlgId = AlgorithmId.get(sigEngine.getAlgorithm());
infoSigAlgId = sigAlgId;
DerOutputStream out = new DerOutputStream();
DerOutputStream tmp = new DerOutputStream();
// encode crl info
encodeInfo(tmp);
// encode algorithm identifier
sigAlgId.encode(tmp);
// Create and encode the signature itself.
sigEngine.update(tbsCertList, 0, tbsCertList.length);
signature = sigEngine.sign();
tmp.putBitString(signature);
// Wrap the signed data in a SEQUENCE { data, algorithm, sig }
out.write(DerValue.tag_Sequence, tmp);
signedCRL = out.toByteArray();
readOnly = true;
} catch (IOException e) {
throw new CRLException("Error while encoding data: " +
e.getMessage());
}
}
示例14: testWolfSignInitMulti
import java.security.Signature; //導入方法依賴的package包/類
@Test
public void testWolfSignInitMulti()
throws NoSuchProviderException, NoSuchAlgorithmException,
SignatureException, InvalidKeyException,
InvalidAlgorithmParameterException {
String toSign = "Hello World";
byte[] toSignBuf = toSign.getBytes();
byte[] signature = null;
for (int i = 0; i < wolfJCEAlgos.length; i++) {
Signature signer =
Signature.getInstance(wolfJCEAlgos[i], "wolfJCE");
Signature verifier =
Signature.getInstance(wolfJCEAlgos[i], "wolfJCE");
assertNotNull(signer);
assertNotNull(verifier);
SecureRandom rand =
SecureRandom.getInstance("HashDRBG", "wolfJCE");
assertNotNull(rand);
/* generate key pair */
KeyPair pair = generateKeyPair(wolfJCEAlgos[i], rand);
assertNotNull(pair);
PrivateKey priv = pair.getPrivate();
PublicKey pub = pair.getPublic();
/* test multiple inits on signer */
signer.initSign(priv);
signer.initSign(priv);
/* test multiple inits on verifier */
verifier.initVerify(pub);
verifier.initVerify(pub);
/* make sure sign/verify still work after multi init */
signer.update(toSignBuf, 0, toSignBuf.length);
signature = signer.sign();
verifier.update(toSignBuf, 0, toSignBuf.length);
boolean verified = verifier.verify(signature);
if (verified != true) {
fail("Signature verification failed when generating with " +
"wolfJCE and verifying with system default JCE " +
"provider");
}
}
}
示例15: getUserSig
import java.security.Signature; //導入方法依賴的package包/類
@Override
public String getUserSig(String identifier, long expire)throws QCloudException {
try {
Security.addProvider(new BouncyCastleProvider());
Reader reader = new CharArrayReader(imConfig.getPrivateKey().toCharArray());
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
PEMParser parser = new PEMParser(reader);
Object obj = parser.readObject();
parser.close();
PrivateKey privKeyStruct = converter.getPrivateKey((PrivateKeyInfo) obj);
String jsonString = "{"
+ "\"TLS.account_type\":\"" + 0 +"\","
+"\"TLS.identifier\":\"" + identifier +"\","
+"\"TLS.appid_at_3rd\":\"" + 0 +"\","
+"\"TLS.sdk_appid\":\"" + imConfig.getSdkAppId() +"\","
+"\"TLS.expire_after\":\"" + expire +"\","
+"\"TLS.version\": \"201512300000\""
+"}";
String time = String.valueOf(System.currentTimeMillis()/1000);
String SerialString =
"TLS.appid_at_3rd:" + 0 + "\n" +
"TLS.account_type:" + 0 + "\n" +
"TLS.identifier:" + identifier + "\n" +
"TLS.sdk_appid:" + imConfig.getSdkAppId() + "\n" +
"TLS.time:" + time + "\n" +
"TLS.expire_after:" + expire +"\n";
//Create Signature by SerialString
Signature signature = Signature.getInstance("SHA256withECDSA", "BC");
signature.initSign(privKeyStruct);
signature.update(SerialString.getBytes(Charset.forName("UTF-8")));
byte[] signatureBytes = signature.sign();
String sigTLS = Base64.encodeBase64String(signatureBytes);
//Add TlsSig to jsonString
JSONObject jsonObject= JSON.parseObject(jsonString);
jsonObject.put("TLS.sig", (Object)sigTLS);
jsonObject.put("TLS.time", (Object)time);
jsonString = jsonObject.toString();
//compression
Deflater compresser = new Deflater();
compresser.setInput(jsonString.getBytes(Charset.forName("UTF-8")));
compresser.finish();
byte [] compressBytes = new byte [512];
int compressBytesLength = compresser.deflate(compressBytes);
compresser.end();
return new String(Base64Url.base64EncodeUrl(Arrays.copyOfRange(compressBytes,0,compressBytesLength)));
}catch (Exception e) {
throw new QCloudException(e);
}
}