本文整理汇总了Java中javax.xml.crypto.dsig.SignatureMethod类的典型用法代码示例。如果您正苦于以下问题:Java SignatureMethod类的具体用法?Java SignatureMethod怎么用?Java SignatureMethod使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SignatureMethod类属于javax.xml.crypto.dsig包,在下文中一共展示了SignatureMethod类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: algEquals
import javax.xml.crypto.dsig.SignatureMethod; //导入依赖的package包/类
static boolean algEquals ( final String algURI, final String algName )
{
if ( algName.equalsIgnoreCase ( "DSA" ) && algURI.equalsIgnoreCase ( SignatureMethod.DSA_SHA1 ) )
{
return true;
}
else if ( algName.equalsIgnoreCase ( "RSA" ) && algURI.equalsIgnoreCase ( SignatureMethod.RSA_SHA1 ) )
{
return true;
}
else
{
logger.warn ( "Failed to check key - algUri: {}, algName: {}", algURI, algName );
return false;
}
}
示例2: algEquals
import javax.xml.crypto.dsig.SignatureMethod; //导入依赖的package包/类
static boolean algEquals ( final String algURI, final String algName )
{
if ( algName.equalsIgnoreCase ( "DSA" ) && algURI.equalsIgnoreCase ( SignatureMethod.DSA_SHA1 ) )
{
return true;
}
else if ( algName.equalsIgnoreCase ( "RSA" ) && algURI.equalsIgnoreCase ( SignatureMethod.RSA_SHA1 ) )
{
return true;
}
else
{
logger.trace ( "Failed to check key - algUri: {}, algName: {}", algURI, algName );
return false;
}
}
示例3: fromAlg
import javax.xml.crypto.dsig.SignatureMethod; //导入依赖的package包/类
private String fromAlg ( final String alg )
{
if ( "DSA".equals ( alg ) )
{
return SignatureMethod.DSA_SHA1;
}
else if ( "RSA".equals ( alg ) )
{
return SignatureMethod.RSA_SHA1;
}
else if ( "HMAC".equals ( alg ) )
{
return SignatureMethod.HMAC_SHA1;
}
else
{
throw new IllegalArgumentException ( String.format ( "Key algorithm '%s' is not supported", alg ) );
}
}
示例4: select_publicKey_exception
import javax.xml.crypto.dsig.SignatureMethod; //导入依赖的package包/类
@Test()
public void select_publicKey_exception() throws Exception {
// given
selector = spy(new X509KeySelector(keystore));
KeyInfo keyinfo = mock(KeyInfo.class);
ArrayList<XMLStructure> list = new ArrayList<XMLStructure>();
X509Data x509Data = mock(X509Data.class);
list.add(x509Data);
doReturn(list).when(keyinfo).getContent();
ArrayList<Object> x509DataContent = new ArrayList<Object>();
x509DataContent.add(mock(X509Certificate.class));
doReturn(x509DataContent).when(x509Data).getContent();
doThrow(new KeyStoreException("key exception")).when(selector)
.getPublicKeyFromKeystore(any(X509Certificate.class),
any(SignatureMethod.class));
// when
try {
selector.select(keyinfo, null, null, null);
fail();
} catch (KeySelectorException e) {
assertTrue(e.getCause().getMessage().contains("key exception"));
}
}
示例5: getSignatureMethod
import javax.xml.crypto.dsig.SignatureMethod; //导入依赖的package包/类
private String getSignatureMethod(DigestAlgo digestAlgo) {
if (null == digestAlgo) {
throw new RuntimeException("digest algo is null");
}
switch (digestAlgo) {
case SHA1:
return SignatureMethod.RSA_SHA1;
case SHA256:
return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA256;
case SHA512:
return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA512;
}
// TODO: complete me
// if ("SHA-384".equals(digestAlgo)) {
// return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA384;
// }
// if ("RIPEMD160".equals(digestAlgo)) {
// return XMLSignature.ALGO_ID_SIGNATURE_RSA_RIPEMD160;
// }
throw new RuntimeException("unsupported sign algo: " + digestAlgo);
}
示例6: getSignatureMethod
import javax.xml.crypto.dsig.SignatureMethod; //导入依赖的package包/类
private String getSignatureMethod(String digestAlgo) {
if (null == digestAlgo) {
throw new RuntimeException("digest algo is null");
}
if ("SHA-1".equals(digestAlgo)) {
return SignatureMethod.RSA_SHA1;
}
if ("SHA-256".equals(digestAlgo)) {
return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA256;
}
if ("SHA-512".equals(digestAlgo)) {
return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA512;
}
if ("SHA-384".equals(digestAlgo)) {
return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA384;
}
if ("RIPEMD160".equals(digestAlgo)) {
return XMLSignature.ALGO_ID_SIGNATURE_RSA_RIPEMD160;
}
throw new RuntimeException("unsupported sign algo: " + digestAlgo);
}
示例7: validate_dsa_publicKey
import javax.xml.crypto.dsig.SignatureMethod; //导入依赖的package包/类
@Test
public void validate_dsa_publicKey() throws Exception {
// given
XMLSignatureBuilder builder = new XMLSignatureBuilder(
SignatureMethod.DSA_SHA1);
KeyPair keyPair = generateKeyPair("DSA");
FileInputStream in = null;
Document document = null;
try {
in = new FileInputStream(FILE_UNSIGNED_ASSERTION);
document = builder.sign(in, keyPair);
} finally {
if (in != null) {
in.close();
}
}
NodeList nl = document.getElementsByTagNameNS(XMLSignature.XMLNS,
"Signature");
// when
boolean valid = validator.validate(nl.item(0), keyPair.getPublic());
// then
assertTrue(valid);
}
示例8: validate_dsa_keySelector
import javax.xml.crypto.dsig.SignatureMethod; //导入依赖的package包/类
@Test
public void validate_dsa_keySelector() throws Exception {
// given
XMLSignatureBuilder builder = new XMLSignatureBuilder(
SignatureMethod.DSA_SHA1);
KeyPair keyPair = generateKeyPair("DSA");
FileInputStream in = null;
Document document = null;
try {
in = new FileInputStream(FILE_UNSIGNED_ASSERTION);
document = builder.sign(in, keyPair);
} finally {
if (in != null) {
in.close();
}
}
NodeList nl = document.getElementsByTagNameNS(XMLSignature.XMLNS,
"Signature");
// when
boolean valid = validator.validate(nl.item(0));
// then
assertTrue(valid);
}
示例9: sign
import javax.xml.crypto.dsig.SignatureMethod; //导入依赖的package包/类
public <T extends Node> T sign(T node) {
checkNotNull(node);
checkArgument(node instanceof Document || node instanceof Element);
try {
Element element = node instanceof Document ? ((Document) node).getDocumentElement() : (Element) node;
DOMSignContext dsc = new DOMSignContext(privateKey, element);
XMLSignatureFactory signatureFactory = XMLSignatureFactory.getInstance("DOM");
List<Transform> transformList = new LinkedList<>();
transformList.add(signatureFactory.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null));
transformList.add(signatureFactory.newTransform(C14N_TRANSFORM_METHOD, (TransformParameterSpec) null));
Node child = findFirstElementChild(element);
((Element) child).setIdAttribute("Id", true);
String id = child.getAttributes().getNamedItem("Id").getNodeValue();
String uri = String.format("#%s", id);
Reference reference = signatureFactory.newReference(uri,
signatureFactory.newDigestMethod(DigestMethod.SHA1, null), transformList, null, null);
SignedInfo signedInfo = signatureFactory.newSignedInfo(signatureFactory.newCanonicalizationMethod(
CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null), signatureFactory
.newSignatureMethod(SignatureMethod.RSA_SHA1, null), Collections.singletonList(reference));
KeyInfoFactory kif = signatureFactory.getKeyInfoFactory();
X509Data x509Data = kif.newX509Data(Collections.singletonList(certificateChain[0]));
KeyInfo keyInfo = kif.newKeyInfo(Collections.singletonList(x509Data));
XMLSignature xmlSignature = signatureFactory.newXMLSignature(signedInfo, keyInfo);
xmlSignature.sign(dsc);
return node;
}
catch (Exception ex) {
throw new IllegalArgumentException("Erro ao assinar XML.", ex);
}
}
示例10: testSymmetricSignatureSHA1
import javax.xml.crypto.dsig.SignatureMethod; //导入依赖的package包/类
/**
* Test signing a message body using a symmetric key with EncryptedKeySHA1
*/
public void testSymmetricSignatureSHA1() throws Exception {
SOAPEnvelope unsignedEnvelope = message.getSOAPEnvelope();
Document doc = unsignedEnvelope.getAsDocument();
WSSecHeader secHeader = new WSSecHeader();
secHeader.insertSecurityHeader(doc);
WSSecSignature sign = new WSSecSignature();
sign.setKeyIdentifierType(WSConstants.ENCRYPTED_KEY_SHA1_IDENTIFIER);
sign.setSecretKey(keyData);
sign.setSignatureAlgorithm(SignatureMethod.HMAC_SHA1);
Document signedDoc = sign.build(doc, crypto, secHeader);
if (LOG.isDebugEnabled()) {
LOG.debug("Signed symmetric message SHA1:");
String outputString =
org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(signedDoc);
LOG.debug(outputString);
}
verify(signedDoc);
}
示例11: certSelect
import javax.xml.crypto.dsig.SignatureMethod; //导入依赖的package包/类
/**
* Searches the specified keystore for a certificate that matches the
* specified X509Certificate and contains a public key that is compatible
* with the specified SignatureMethod.
*
* @return a KeySelectorResult containing the cert's public key if there
* is a match; otherwise null
*/
private KeySelectorResult certSelect(X509Certificate xcert,
SignatureMethod sm) throws KeyStoreException {
// skip non-signer certs
boolean[] keyUsage = xcert.getKeyUsage();
if (!keyUsage[0]) {
return null;
}
String alias = ks.getCertificateAlias(xcert);
if (alias != null) {
PublicKey pk = ks.getCertificate(alias).getPublicKey();
// make sure algorithm is compatible with method
if (algEquals(sm.getAlgorithm(), pk.getAlgorithm())) {
return new SimpleKeySelectorResult(pk);
}
}
return null;
}
示例12: XmlSignatureHandler
import javax.xml.crypto.dsig.SignatureMethod; //导入依赖的package包/类
public XmlSignatureHandler() throws NoSuchAlgorithmException,
InvalidAlgorithmParameterException {
this.builderFactory = DocumentBuilderFactory.newInstance();
this.builderFactory.setNamespaceAware(true);
this.transformerFactory = TransformerFactory.newInstance();
this.signatureFactory = XMLSignatureFactory.getInstance("DOM");
this.digestMethod = signatureFactory.newDigestMethod(DigestMethod.SHA1, null);
this.transformList = new ArrayList<Transform>(2);
this.transformList.add(
signatureFactory.newTransform(
Transform.ENVELOPED,
(TransformParameterSpec) null));
this.transformList.add(
signatureFactory.newTransform(
"http://www.w3.org/TR/2001/REC-xml-c14n-20010315",
(TransformParameterSpec) null));
this.canonicalizationMethod = this.signatureFactory.newCanonicalizationMethod(
CanonicalizationMethod.INCLUSIVE,
(C14NMethodParameterSpec) null);
this.signatureMethod = this.signatureFactory.newSignatureMethod(SignatureMethod.RSA_SHA1, null);
this.keyInfoFactory = this.signatureFactory.getKeyInfoFactory();
}
示例13: signSamlElement
import javax.xml.crypto.dsig.SignatureMethod; //导入依赖的package包/类
/**
* Sign SAML element.
*
* @param element the element
* @param privKey the priv key
* @param pubKey the pub key
* @return the element
*/
private static org.jdom.Element signSamlElement(final org.jdom.Element element, final PrivateKey privKey, final PublicKey pubKey) {
try {
final String providerName = System.getProperty("jsr105Provider", SIGNATURE_FACTORY_PROVIDER_CLASS);
final XMLSignatureFactory sigFactory = XMLSignatureFactory
.getInstance("DOM", (Provider) Class.forName(providerName).newInstance());
final List<Transform> envelopedTransform = Collections.singletonList(sigFactory.newTransform(Transform.ENVELOPED,
(TransformParameterSpec) null));
final Reference ref = sigFactory.newReference(StringUtils.EMPTY, sigFactory
.newDigestMethod(DigestMethod.SHA1, null), envelopedTransform, null, null);
// Create the SignatureMethod based on the type of key
final SignatureMethod signatureMethod;
final String algorithm = pubKey.getAlgorithm();
switch (algorithm) {
case "DSA":
signatureMethod = sigFactory.newSignatureMethod(SignatureMethod.DSA_SHA1, null);
break;
case "RSA":
signatureMethod = sigFactory.newSignatureMethod(SignatureMethod.RSA_SHA1, null);
break;
default:
throw new RuntimeException("Error signing SAML element: Unsupported type of key");
}
final CanonicalizationMethod canonicalizationMethod = sigFactory
.newCanonicalizationMethod(
CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS,
(C14NMethodParameterSpec) null);
// Create the SignedInfo
final SignedInfo signedInfo = sigFactory.newSignedInfo(
canonicalizationMethod, signatureMethod, Collections.singletonList(ref));
// Create a KeyValue containing the DSA or RSA PublicKey
final KeyInfoFactory keyInfoFactory = sigFactory.getKeyInfoFactory();
final KeyValue keyValuePair = keyInfoFactory.newKeyValue(pubKey);
// Create a KeyInfo and add the KeyValue to it
final KeyInfo keyInfo = keyInfoFactory.newKeyInfo(Collections.singletonList(keyValuePair));
// Convert the JDOM document to w3c (Java XML signature API requires w3c representation)
final Element w3cElement = toDom(element);
// Create a DOMSignContext and specify the DSA/RSA PrivateKey and
// location of the resulting XMLSignature's parent element
final DOMSignContext dsc = new DOMSignContext(privKey, w3cElement);
final Node xmlSigInsertionPoint = getXmlSignatureInsertLocation(w3cElement);
dsc.setNextSibling(xmlSigInsertionPoint);
// Marshal, generate (and sign) the enveloped signature
final XMLSignature signature = sigFactory.newXMLSignature(signedInfo, keyInfo);
signature.sign(dsc);
return toJdom(w3cElement);
} catch (final Exception e) {
throw new RuntimeException("Error signing SAML element: " + e.getMessage(), e);
}
}
示例14: select
import javax.xml.crypto.dsig.SignatureMethod; //导入依赖的package包/类
@Override
public KeySelectorResult select ( final KeyInfo keyInfo, final KeySelector.Purpose purpose, final AlgorithmMethod method, final XMLCryptoContext context ) throws KeySelectorException
{
if ( keyInfo == null )
{
throw new KeySelectorException ( "Null KeyInfo object!" );
}
final SignatureMethod sm = (SignatureMethod)method;
final List<?> list = keyInfo.getContent ();
for ( int i = 0; i < list.size (); i++ )
{
final XMLStructure xmlStructure = (XMLStructure)list.get ( i );
if ( xmlStructure instanceof KeyValue )
{
try
{
final PublicKey pk = ( (KeyValue)xmlStructure ).getPublicKey ();
// make sure algorithm is compatible with method
if ( algEquals ( sm.getAlgorithm (), pk.getAlgorithm () ) )
{
return new SimpleKeySelectorResult ( pk );
}
}
catch ( final KeyException ke )
{
throw new KeySelectorException ( ke );
}
}
}
throw new KeySelectorException ( "No KeyValue element found!" );
}
示例15: select
import javax.xml.crypto.dsig.SignatureMethod; //导入依赖的package包/类
@Override
public KeySelectorResult select ( final KeyInfo keyInfo, final KeySelector.Purpose purpose, final AlgorithmMethod method, final XMLCryptoContext context ) throws KeySelectorException
{
if ( keyInfo == null )
{
throw new KeySelectorException ( "Null KeyInfo object!" );
}
final SignatureMethod sm = (SignatureMethod)method;
final List<?> list = keyInfo.getContent ();
for ( final Object l : list )
{
final XMLStructure xmlStructure = (XMLStructure)l;
if ( xmlStructure instanceof X509Data )
{
for ( final Object o : ( (X509Data)xmlStructure ).getContent () )
{
KeySelectorResult result = null;
if ( o instanceof X509Certificate )
{
result = findPublicKey ( (X509Certificate)o, sm );
}
if ( result != null )
{
return result;
}
}
}
}
throw new KeySelectorException ( "No KeyValue element found!" );
}