use of java.security.interfaces.DSAPublicKey in project robovm by robovm.
the class DSAPublicKeyTest method test_getY.
/**
* java.security.interfaces.DSAPublicKey
* #getY()
* test covers following use cases
* Case 1: check with predefined p, q, g, x
* Case 2: check with random p, q, g, x. It takes some time (up to
* minute)
*/
public void test_getY() throws Exception {
KeyPairGenerator keyGen = null;
KeyPair keys = null;
DSAPrivateKey priv = null;
DSAPublicKey publ = null;
// Case 1: check with predefined p, q, g, x
keyGen = KeyPairGenerator.getInstance("DSA");
keyGen.initialize(new DSAParameterSpec(Util.P, Util.Q, Util.G), new SecureRandom(new MySecureRandomSpi(), null) {
});
keys = keyGen.generateKeyPair();
priv = (DSAPrivateKey) keys.getPrivate();
publ = (DSAPublicKey) keys.getPublic();
assertNotNull("Invalid Y value", publ.getY());
// Case 2: check with random p, q, g, x. It takes some time (up to
// minute)
keyGen = KeyPairGenerator.getInstance("DSA");
keys = keyGen.generateKeyPair();
priv = (DSAPrivateKey) keys.getPrivate();
publ = (DSAPublicKey) keys.getPublic();
assertNotNull("Invalid Y value", publ.getY());
}
use of java.security.interfaces.DSAPublicKey in project robovm by robovm.
the class KeyPairGenerator4Test method test_initializeLjava_security_spec_AlgorithmParameterSpec.
/**
* java.security.KeyPairGenerator#initialize(java.security.spec.AlgorithmParameterSpec)
*/
public void test_initializeLjava_security_spec_AlgorithmParameterSpec() throws Exception {
// create DSAParams
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA");
keyPairGenerator.initialize(1024);
DSAPublicKey key = (DSAPublicKey) keyPairGenerator.genKeyPair().getPublic();
DSAParams params = key.getParams();
KeyPairGenerator keyPair = KeyPairGenerator.getInstance("DSA");
keyPair.initialize(new DSAParameterSpec(params.getP(), params.getQ(), params.getG()));
}
use of java.security.interfaces.DSAPublicKey in project robovm by robovm.
the class KeyPairGenerator4Test method test_initializeLjava_security_spec_AlgorithmParameterSpecLjava_security_SecureRandom.
/**
* java.security.KeyPairGenerator#initialize(java.security.spec.AlgorithmParameterSpec,
* java.security.SecureRandom)
*/
public void test_initializeLjava_security_spec_AlgorithmParameterSpecLjava_security_SecureRandom() throws Exception {
// create DSAParams
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA");
keyPairGenerator.initialize(1024);
DSAPublicKey key = (DSAPublicKey) keyPairGenerator.genKeyPair().getPublic();
DSAParams params = key.getParams();
KeyPairGenerator keyPair = KeyPairGenerator.getInstance("DSA");
keyPair.initialize(new DSAParameterSpec(params.getP(), params.getQ(), params.getG()), new SecureRandom());
}
use of java.security.interfaces.DSAPublicKey in project Conversations by siacs.
the class Conversation method getOtrFingerprint.
public synchronized String getOtrFingerprint() {
if (this.otrFingerprint == null) {
try {
if (getOtrSession() == null || getOtrSession().getSessionStatus() != SessionStatus.ENCRYPTED) {
return null;
}
DSAPublicKey remotePubKey = (DSAPublicKey) getOtrSession().getRemotePublicKey();
this.otrFingerprint = getAccount().getOtrService().getFingerprint(remotePubKey).toLowerCase(Locale.US);
} catch (final OtrCryptoException | UnsupportedOperationException ignored) {
return null;
}
}
return this.otrFingerprint;
}
use of java.security.interfaces.DSAPublicKey in project carat by amplab.
the class SamplingLibrary method getSignatures.
public static List<String> getSignatures(PackageInfo pak) {
List<String> sigList = new LinkedList<String>();
String[] pmInfos = pak.requestedPermissions;
if (pmInfos != null) {
byte[] bytes = getPermissionBytes(pmInfos);
String hexB = convertToHex(bytes);
sigList.add(hexB);
}
Signature[] sigs = pak.signatures;
for (Signature s : sigs) {
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA-1");
md.update(s.toByteArray());
byte[] dig = md.digest();
// Add SHA-1
sigList.add(convertToHex(dig));
CertificateFactory fac = CertificateFactory.getInstance("X.509");
if (fac == null)
continue;
X509Certificate cert = (X509Certificate) fac.generateCertificate(new ByteArrayInputStream(s.toByteArray()));
if (cert == null)
continue;
PublicKey pkPublic = cert.getPublicKey();
if (pkPublic == null)
continue;
String al = pkPublic.getAlgorithm();
if (al.equals("RSA")) {
md = MessageDigest.getInstance("SHA-256");
RSAPublicKey rsa = (RSAPublicKey) pkPublic;
byte[] data = rsa.getModulus().toByteArray();
if (data[0] == 0) {
byte[] copy = new byte[data.length - 1];
System.arraycopy(data, 1, copy, 0, data.length - 1);
md.update(copy);
} else
md.update(data);
dig = md.digest();
// Add SHA-256 of modulus
sigList.add(convertToHex(dig));
} else if (al.equals("DSA")) {
DSAPublicKey dsa = (DSAPublicKey) pkPublic;
md = MessageDigest.getInstance("SHA-256");
byte[] data = dsa.getY().toByteArray();
if (data[0] == 0) {
byte[] copy = new byte[data.length - 1];
System.arraycopy(data, 1, copy, 0, data.length - 1);
md.update(copy);
} else
md.update(data);
dig = md.digest();
// Add SHA-256 of public key (DSA)
sigList.add(convertToHex(dig));
} else {
Log.e("SamplingLibrary", "Weird algorithm: " + al + " for " + pak.packageName);
}
} catch (NoSuchAlgorithmException e) {
// Do nothing
} catch (CertificateException e) {
// Do nothing
}
}
return sigList;
}
Aggregations