use of java.security.Signature in project android_frameworks_base by ResurrectionRemix.
the class CertPinInstallReceiverTest method createSignature.
private String createSignature(String content, String version, String requiredHash) throws Exception {
Signature signer = Signature.getInstance("SHA512withRSA");
signer.initSign(createKey());
signer.update(content.trim().getBytes());
signer.update(version.trim().getBytes());
signer.update(requiredHash.getBytes());
String sig = new String(Base64.encode(signer.sign(), Base64.DEFAULT));
assertEquals(true, verifySignature(content, version, requiredHash, sig, createCertificate()));
return sig;
}
use of java.security.Signature in project android_frameworks_base by ResurrectionRemix.
the class CertPinInstallReceiverTest method verifySignature.
public boolean verifySignature(String content, String version, String requiredPrevious, String signature, X509Certificate cert) throws Exception {
Signature signer = Signature.getInstance("SHA512withRSA");
signer.initVerify(cert);
signer.update(content.trim().getBytes());
signer.update(version.trim().getBytes());
signer.update(requiredPrevious.trim().getBytes());
return signer.verify(Base64.decode(signature.getBytes(), Base64.DEFAULT));
}
use of java.security.Signature in project robovm by robovm.
the class X509Util method calculateSignature.
static byte[] calculateSignature(DERObjectIdentifier sigOid, String sigName, String provider, PrivateKey key, SecureRandom random, ASN1Encodable object) throws IOException, NoSuchProviderException, NoSuchAlgorithmException, InvalidKeyException, SignatureException {
Signature sig;
if (sigOid == null) {
throw new IllegalStateException("no signature algorithm specified");
}
sig = X509Util.getSignatureInstance(sigName, provider);
if (random != null) {
sig.initSign(key, random);
} else {
sig.initSign(key);
}
sig.update(object.toASN1Primitive().getEncoded(ASN1Encoding.DER));
return sig.sign();
}
use of java.security.Signature in project GNS by MobilityFirst.
the class CryptoUtils method signDigestOfMessage.
/**
* Signs a digest of a message using private key of the given guid.
*
* @param guidEntry
* @param message
* @return a signed digest of the message string encoded as a hex string
* @throws InvalidKeyException
* @throws NoSuchAlgorithmException
* @throws SignatureException
* @throws java.io.UnsupportedEncodingException
*
* arun: This method need to be synchronized over the signature
* instance, otherwise it will result in corrupted signatures.
*/
public static String signDigestOfMessage(GuidEntry guidEntry, String message) throws ClientException {
try {
Signature signatureInstance = getSignatureInstance();
synchronized (signatureInstance) {
signatureInstance.initSign(guidEntry.getPrivateKey());
// iOS client uses UTF-8 - should switch to ISO-8859-1 to be consistent with
// secret key version
signatureInstance.update(message.getBytes("UTF-8"));
byte[] signedString = signatureInstance.sign();
// We used to encode this as a hex so we could send it with the html without
// encoding. Not really necessary anymore for the socket based client,
// but the iOS client does as well so we need to keep it like this.
// Also note that the secret based method doesn't do this - it just returns a string
// using the ISO-8859-1 charset.
String result = DatatypeConverter.printHexBinary(signedString);
//String result = ByteUtils.toHex(signedString);
return result;
}
} catch (InvalidKeyException | UnsupportedEncodingException | SignatureException e) {
throw new ClientException("Error encoding message", e);
}
}
use of java.security.Signature in project nhin-d by DirectProject.
the class PKCS11OperationTests method testSignDataOnToken.
@Test
public void testSignDataOnToken() throws Exception {
final String pkcs11ProvName = TestUtils.setupSafeNetToken();
if (!StringUtils.isEmpty(pkcs11ProvName)) {
final KeyStore ks = KeyStore.getInstance("PKCS11");
ks.load(null, "1Kingpuff".toCharArray());
final Enumeration<String> aliases = ks.aliases();
while (aliases.hasMoreElements()) {
final String alias = aliases.nextElement();
System.out.println("\r\nAlias Name: " + alias);
final KeyStore.Entry entry = ks.getEntry(alias, null);
System.out.println("Key Type: " + entry.getClass());
if (entry instanceof KeyStore.PrivateKeyEntry) {
final KeyStore.PrivateKeyEntry pEntry = (KeyStore.PrivateKeyEntry) entry;
final Signature sig = Signature.getInstance("SHA256withRSA", pkcs11ProvName);
sig.initSign(pEntry.getPrivateKey());
// sign the data
String starttext = "Some Text to Encrypt and Sign as an Example";
final byte[] bytes = starttext.getBytes();
sig.update(bytes);
final byte[] theSignature = sig.sign();
assertNotNull(theSignature);
System.out.println("Data Signed");
break;
}
}
}
}
Aggregations