use of java.security.Signature in project cloudstack by apache.
the class SAMLUtils method generateSAMLRequestSignature.
public static String generateSAMLRequestSignature(final String urlEncodedString, final PrivateKey signingKey, final String sigAlgorithmName) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException, UnsupportedEncodingException {
if (signingKey == null) {
return urlEncodedString;
}
String opensamlAlgoIdSignature = SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA1;
String javaSignatureAlgorithmName = "SHA1withRSA";
if (sigAlgorithmName.equalsIgnoreCase("SHA256")) {
opensamlAlgoIdSignature = SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA256;
javaSignatureAlgorithmName = "SHA256withRSA";
} else if (sigAlgorithmName.equalsIgnoreCase("SHA384")) {
opensamlAlgoIdSignature = SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA384;
javaSignatureAlgorithmName = "SHA384withRSA";
} else if (sigAlgorithmName.equalsIgnoreCase("SHA512")) {
opensamlAlgoIdSignature = SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA512;
javaSignatureAlgorithmName = "SHA512withRSA";
}
String url = urlEncodedString + "&SigAlg=" + URLEncoder.encode(opensamlAlgoIdSignature, HttpUtils.UTF_8);
Signature signature = Signature.getInstance(javaSignatureAlgorithmName);
signature.initSign(signingKey);
signature.update(url.getBytes(Charset.forName("UTF-8")));
String signatureString = Base64.encodeBytes(signature.sign(), Base64.DONT_BREAK_LINES);
if (signatureString != null) {
return url + "&Signature=" + URLEncoder.encode(signatureString, HttpUtils.UTF_8);
}
return url;
}
use of java.security.Signature in project robovm by robovm.
the class Signature2Test method test_clone.
/**
* java.security.Signature#clone()
*/
public void test_clone() throws Exception {
Signature s = Signature.getInstance("DSA");
try {
s.clone();
fail();
} catch (CloneNotSupportedException expected) {
}
}
use of java.security.Signature in project robovm by robovm.
the class Signature2Test method test_getParameters.
/**
* java.security.Signature#getParameters()
*/
public void test_getParameters() throws Exception {
Signature sig = Signature.getInstance("DSA");
try {
sig.getParameters();
} catch (UnsupportedOperationException e) {
// Could be that the operation is not supported
}
try {
MySignature sig2 = new MySignature("test");
sig2.getParameters();
fail();
} catch (UnsupportedOperationException expected) {
}
MySignature sig2 = new MySignature("ABC");
sig2.getParameters();
}
use of java.security.Signature in project robovm by robovm.
the class Signature2Test method test_setParameterLjava_lang_StringLjava_lang_Object.
/**
* java.security.Signature#setParameter(java.lang.String,
* java.lang.Object)
*/
@SuppressWarnings("deprecation")
public void test_setParameterLjava_lang_StringLjava_lang_Object() throws Exception {
Signature sig = Signature.getInstance("DSA");
try {
sig.setParameter("r", BigInteger.ONE);
sig.setParameter("s", BigInteger.ONE);
} catch (InvalidParameterException e) {
// Could be that it's an invalid param for the found algorithm
} catch (UnsupportedOperationException e) {
// Could be that the operation is not supported
}
}
use of java.security.Signature in project robovm by robovm.
the class Signature2Test method test_verify$BII.
/**
* java.security.Signature#verify(byte[], int, int)
*/
public void test_verify$BII() throws Exception {
Signature sig = Signature.getInstance("DSA");
sig.initSign(getDsaKeys().getPrivate());
sig.update(MESSAGE.getBytes());
byte[] signature = sig.sign();
sig.initVerify(getDsaKeys().getPublic());
sig.update(MESSAGE.getBytes());
assertTrue("Sign/Verify does not pass", sig.verify(signature, 0, signature.length));
try {
sig.verify(null, 0, signature.length);
fail();
} catch (IllegalArgumentException expected) {
}
try {
sig.verify(signature, -5, signature.length);
fail();
} catch (IllegalArgumentException expected) {
}
if (StandardNames.IS_RI) {
try {
sig.verify(signature, signature.length, 0);
fail();
} catch (SignatureException expected) {
}
} else {
// Calling Signature.verify a second time should not throw
// http://code.google.com/p/android/issues/detail?id=34933
boolean verified = sig.verify(signature, signature.length, 0);
assertFalse(verified);
}
try {
sig.verify(signature, 0, signature.length * 2);
fail();
} catch (IllegalArgumentException expected) {
}
}
Aggregations