org.apache.xml.security.utils.Base64.encode()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(14.2k)|赞(0)|评价(0)|浏览(156)

本文整理了Java中org.apache.xml.security.utils.Base64.encode()方法的一些代码示例,展示了Base64.encode()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Base64.encode()方法的具体详情如下:
包路径:org.apache.xml.security.utils.Base64
类名称:Base64
方法名:encode

Base64.encode介绍

[英]Encode in Base64 the given BigInteger.
[中]在Base64中编码给定的BigInteger

代码示例

代码示例来源:origin: org.apache.santuario/xmlsec

/**
 * Encode a byte array and fold lines at the standard 76th character unless
 * ignore line breaks property is set.
 *
 * @param binaryData <code>byte[]</code> to be base64 encoded
 * @return the <code>String</code> with encoded data
 */
public static final String encode(byte[] binaryData) {
  return XMLUtils.ignoreLineBreaks()
    ? encode(binaryData, Integer.MAX_VALUE)
    : encode(binaryData, BASE64DEFAULTLENGTH);
}

代码示例来源:origin: org.wso2.carbon.identity/org.wso2.carbon.identity.core

public static String getHMAC(String secretKey, String baseString) throws SignatureException {
  try {
    SecretKeySpec key = new SecretKeySpec(secretKey.getBytes(), HMAC_SHA1_ALGORITHM);
    Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
    mac.init(key);
    byte[] rawHmac = mac.doFinal(baseString.getBytes());
    return Base64.encode(rawHmac);
  } catch (Exception e) {
    throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
  }
}

代码示例来源:origin: org.apache.santuario/xmlsec

/**
 * Encode in Base64 the given <code>{@link BigInteger}</code>.
 *
 * @param big
 * @return String with Base64 encoding
 */
public static final String encode(BigInteger big) {
  return encode(getBytes(big, big.bitLength()));
}

代码示例来源:origin: pl.edu.icm.yadda/yadda-model

/**
 * Encode a native Java BigInteger type to a base64-encoded ds:CryptoBinary value.
 *
 * @param bigInt the BigInteger value
 * @return the encoded CryptoBinary value
 */
public static final String encodeCryptoBinaryFromBigInteger(BigInteger bigInt) {
  // This code is really complicated, for now just use the Apache xmlsec lib code directly.
  byte[] bigIntBytes = org.apache.xml.security.utils.Base64.encode(bigInt, bigInt.bitLength());
  return Base64.encodeBytes(bigIntBytes);
}

代码示例来源:origin: io.apigee.opensaml/xmltooling

/**
 * Encode a native Java BigInteger type to a base64-encoded ds:CryptoBinary value.
 *
 * @param bigInt the BigInteger value
 * @return the encoded CryptoBinary value
 */
public static final String encodeCryptoBinaryFromBigInteger(BigInteger bigInt) {
  // This code is really complicated, for now just use the Apache xmlsec lib code directly.
  byte[] bigIntBytes = org.apache.xml.security.utils.Base64.encode(bigInt, bigInt.bitLength());
  return Base64.encodeBytes(bigIntBytes);
}

代码示例来源:origin: org.wso2.carbon.identity.framework/org.wso2.carbon.identity.core

public static String getHMAC(String secretKey, String baseString) throws SignatureException {
  try {
    SecretKeySpec key = new SecretKeySpec(secretKey.getBytes(), HMAC_SHA1_ALGORITHM);
    Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
    mac.init(key);
    byte[] rawHmac = mac.doFinal(baseString.getBytes());
    return Base64.encode(rawHmac);
  } catch (Exception e) {
    throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
  }
}

代码示例来源:origin: org.opensaml/xmltooling

/**
 * Encode a native Java BigInteger type to a base64-encoded ds:CryptoBinary value.
 *
 * @param bigInt the BigInteger value
 * @return the encoded CryptoBinary value
 */
public static final String encodeCryptoBinaryFromBigInteger(BigInteger bigInt) {
  // This code is really complicated, for now just use the Apache xmlsec lib code directly.
  byte[] bigIntBytes = org.apache.xml.security.utils.Base64.encode(bigInt, bigInt.bitLength());
  return Base64.encodeBytes(bigIntBytes);
}

代码示例来源:origin: Evolveum/midpoint

private String getSecretKeyDigest(SecretKey key) throws NinjaException {
    MessageDigest sha1;
    try {
      sha1 = MessageDigest.getInstance(KEY_DIGEST_TYPE);
    } catch (NoSuchAlgorithmException ex) {
      throw new NinjaException(ex.getMessage(), ex);
    }

    return Base64.encode(sha1.digest(key.getEncoded()));
  }
}

代码示例来源:origin: wso2/carbon-identity-framework

public static String getHMAC(String secretKey, String baseString) throws SignatureException {
  try {
    SecretKeySpec key = new SecretKeySpec(secretKey.getBytes(), HMAC_SHA1_ALGORITHM);
    Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
    mac.init(key);
    byte[] rawHmac = mac.doFinal(baseString.getBytes());
    return Base64.encode(rawHmac);
  } catch (Exception e) {
    throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
  }
}

代码示例来源:origin: org.apache.santuario/xmlsec

/**
 * Method encodeToElement
 *
 * @param doc
 * @param localName
 * @param bytes
 * @return an Element with the base64 encoded in the text.
 *
 */
public static final Element encodeToElement(Document doc, String localName, byte[] bytes) {
  Element el = XMLUtils.createElementInSignatureSpace(doc, localName);
  Text text = doc.createTextNode(encode(bytes));
  el.appendChild(text);
  return el;
}

代码示例来源:origin: org.apache.santuario/xmlsec

/**
 * This method takes an (empty) Element and a BigInteger and adds the
 * base64 encoded BigInteger to the Element.
 *
 * @param element
 * @param biginteger
 */
public static final void fillElementWithBigInteger(Element element, BigInteger biginteger) {
  String encodedInt = encode(biginteger);
  if (!XMLUtils.ignoreLineBreaks() && encodedInt.length() > BASE64DEFAULTLENGTH) {
    encodedInt = "\n" + encodedInt + "\n";
  }
  Document doc = element.getOwnerDocument();
  Text text = doc.createTextNode(encodedInt);
  element.appendChild(text);
}

代码示例来源:origin: org.wso2.carbon.identity.framework/org.wso2.carbon.identity.core

/**
 * Generates a random number using two UUIDs and HMAC-SHA1
 *
 * @return Random Number generated.
 * @throws IdentityException Exception due to Invalid Algorithm or Invalid Key
 */
public static String getRandomNumber() throws IdentityException {
  try {
    String secretKey = UUIDGenerator.generateUUID();
    String baseString = UUIDGenerator.generateUUID();
    SecretKeySpec key = new SecretKeySpec(secretKey.getBytes(), "HmacSHA1");
    Mac mac = Mac.getInstance("HmacSHA1");
    mac.init(key);
    byte[] rawHmac = mac.doFinal(baseString.getBytes());
    String random = Base64.encode(rawHmac);
    // Registry doesn't have support for these character.
    random = random.replace("/", "_");
    random = random.replace("=", "a");
    random = random.replace("+", "f");
    return random;
  } catch (Exception e) {
    log.error("Error when generating a random number.", e);
    throw IdentityException.error("Error when generating a random number.", e);
  }
}

代码示例来源:origin: org.wso2.carbon.identity/org.wso2.carbon.identity.oauth

/**
 * Generates a random number using two UUIDs and HMAC-SHA1
 *
 * @return generated secure random number
 * @throws IdentityOAuthAdminException Invalid Algorithm or Invalid Key
 */
public static String getRandomNumber() throws IdentityOAuthAdminException {
  try {
    String secretKey = UUIDGenerator.generateUUID();
    String baseString = UUIDGenerator.generateUUID();
    SecretKeySpec key = new SecretKeySpec(secretKey.getBytes(Charsets.UTF_8), ALGORITHM);
    Mac mac = Mac.getInstance(ALGORITHM);
    mac.init(key);
    byte[] rawHmac = mac.doFinal(baseString.getBytes(Charsets.UTF_8));
    String random = Base64.encode(rawHmac);
    // Registry doesn't have support for these character.
    random = random.replace("/", "_");
    random = random.replace("=", "a");
    random = random.replace("+", "f");
    return random;
  } catch (Exception e) {
    throw new IdentityOAuthAdminException("Error when generating a random number.", e);
  }
}

代码示例来源:origin: org.wso2.carbon.identity/org.wso2.carbon.identity.provider

@Override
public void setSignature(String signatureAlgorithm, X509Credential cred) throws IdentityProviderException {
  Signature signature = (Signature) buildXMLObject(Signature.DEFAULT_ELEMENT_NAME);
  signature.setSigningCredential(cred);
  signature.setSignatureAlgorithm(signatureAlgorithm);
  signature.setCanonicalizationAlgorithm(Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS);
  try {
    KeyInfo keyInfo = (KeyInfo) buildXMLObject(KeyInfo.DEFAULT_ELEMENT_NAME);
    X509Data data = (X509Data) buildXMLObject(X509Data.DEFAULT_ELEMENT_NAME);
    X509Certificate cert = (X509Certificate) buildXMLObject(X509Certificate.DEFAULT_ELEMENT_NAME);
    String value = Base64.encode(cred.getEntityCertificate().getEncoded());
    cert.setValue(value);
    data.getX509Certificates().add(cert);
    keyInfo.getX509Datas().add(data);
    signature.setKeyInfo(keyInfo);
  } catch (CertificateEncodingException e) {
    log.error("Failed to get encoded certificate", e);
    throw new IdentityProviderException("Error while getting encoded certificate");
  }
  assertion.setSignature(signature);
  signatureList.add(signature);
}

代码示例来源:origin: org.wso2.carbon.identity.inbound.auth.openid/org.wso2.carbon.identity.provider

@Override
public void setSignature(String signatureAlgorithm, X509Credential cred) throws IdentityProviderException {
  Signature signature = (Signature) buildXMLObject(Signature.DEFAULT_ELEMENT_NAME);
  signature.setSigningCredential(cred);
  signature.setSignatureAlgorithm(signatureAlgorithm);
  signature.setCanonicalizationAlgorithm(Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS);
  try {
    KeyInfo keyInfo = (KeyInfo) buildXMLObject(KeyInfo.DEFAULT_ELEMENT_NAME);
    X509Data data = (X509Data) buildXMLObject(X509Data.DEFAULT_ELEMENT_NAME);
    X509Certificate cert = (X509Certificate) buildXMLObject(X509Certificate.DEFAULT_ELEMENT_NAME);
    String value = Base64.encode(cred.getEntityCertificate().getEncoded());
    cert.setValue(value);
    data.getX509Certificates().add(cert);
    keyInfo.getX509Datas().add(data);
    signature.setKeyInfo(keyInfo);
  } catch (CertificateEncodingException e) {
    log.error("Error while getting the encoded certificate", e);
    throw new IdentityProviderException("Error while getting the encoded certificate");
  }
  assertion.setSignature(signature);
  signatureList.add(signature);
}

代码示例来源:origin: org.wso2.carbon.identity/org.wso2.carbon.identity.provider

@Override
public void setSignature(String signatureAlgorithm, X509Credential cred) throws IdentityProviderException {
  Signature signature = (Signature) buildXMLObject(Signature.DEFAULT_ELEMENT_NAME);
  signature.setSigningCredential(cred);
  signature.setSignatureAlgorithm(signatureAlgorithm);
  signature.setCanonicalizationAlgorithm(Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS);
  try {
    KeyInfo keyInfo = (KeyInfo) buildXMLObject(KeyInfo.DEFAULT_ELEMENT_NAME);
    X509Data data = (X509Data) buildXMLObject(X509Data.DEFAULT_ELEMENT_NAME);
    X509Certificate cert = (X509Certificate) buildXMLObject(X509Certificate.DEFAULT_ELEMENT_NAME);
    String value = Base64.encode(cred.getEntityCertificate().getEncoded());
    cert.setValue(value);
    data.getX509Certificates().add(cert);
    keyInfo.getX509Datas().add(data);
    signature.setKeyInfo(keyInfo);
  } catch (CertificateEncodingException e) {
    log.error("Error while getting the encoded certificate", e);
    throw new IdentityProviderException("Error while getting the encoded certificate");
  }
  assertion.setSignature(signature);
  signatureList.add(signature);
}

代码示例来源:origin: org.wso2.carbon.identity.inbound.auth.openid/org.wso2.carbon.identity.provider

@Override
public void setSignature(String signatureAlgorithm, X509Credential cred) throws IdentityProviderException {
  Signature signature = (Signature) buildXMLObject(Signature.DEFAULT_ELEMENT_NAME);
  signature.setSigningCredential(cred);
  signature.setSignatureAlgorithm(signatureAlgorithm);
  signature.setCanonicalizationAlgorithm(Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS);
  try {
    KeyInfo keyInfo = (KeyInfo) buildXMLObject(KeyInfo.DEFAULT_ELEMENT_NAME);
    X509Data data = (X509Data) buildXMLObject(X509Data.DEFAULT_ELEMENT_NAME);
    X509Certificate cert = (X509Certificate) buildXMLObject(X509Certificate.DEFAULT_ELEMENT_NAME);
    String value = Base64.encode(cred.getEntityCertificate().getEncoded());
    cert.setValue(value);
    data.getX509Certificates().add(cert);
    keyInfo.getX509Datas().add(data);
    signature.setKeyInfo(keyInfo);
  } catch (CertificateEncodingException e) {
    log.error("Failed to get encoded certificate", e);
    throw new IdentityProviderException("Error while getting encoded certificate");
  }
  assertion.setSignature(signature);
  signatureList.add(signature);
}

代码示例来源:origin: org.wso2.carbon.identity.agent.sso.java/org.wso2.carbon.identity.sso.agent

private static Signature setSignatureRaw(String signatureAlgorithm, X509Credential cred) throws SSOAgentException {
  Signature signature = (Signature) buildXMLObject(Signature.DEFAULT_ELEMENT_NAME);
  signature.setSigningCredential(cred);
  signature.setSignatureAlgorithm(signatureAlgorithm);
  signature.setCanonicalizationAlgorithm(Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS);
  try {
    KeyInfo keyInfo = (KeyInfo) buildXMLObject(KeyInfo.DEFAULT_ELEMENT_NAME);
    X509Data data = (X509Data) buildXMLObject(X509Data.DEFAULT_ELEMENT_NAME);
    org.opensaml.xml.signature.X509Certificate cert =
        (org.opensaml.xml.signature.X509Certificate) buildXMLObject(org.opensaml.xml.signature.X509Certificate.DEFAULT_ELEMENT_NAME);
    String value =
        org.apache.xml.security.utils.Base64.encode(cred.getEntityCertificate().getEncoded());
    cert.setValue(value);
    data.getX509Certificates().add(cert);
    keyInfo.getX509Datas().add(data);
    signature.setKeyInfo(keyInfo);
    return signature;
  } catch (CertificateEncodingException e) {
    throw new SSOAgentException("Error getting certificate", e);
  }
}

代码示例来源:origin: org.wso2.carbon.identity/org.wso2.carbon.identity.sso.agent

private static Signature setSignatureRaw(String signatureAlgorithm, X509Credential cred) throws SSOAgentException {
  Signature signature = (Signature) buildXMLObject(Signature.DEFAULT_ELEMENT_NAME);
  signature.setSigningCredential(cred);
  signature.setSignatureAlgorithm(signatureAlgorithm);
  signature.setCanonicalizationAlgorithm(Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS);
  try {
    KeyInfo keyInfo = (KeyInfo) buildXMLObject(KeyInfo.DEFAULT_ELEMENT_NAME);
    X509Data data = (X509Data) buildXMLObject(X509Data.DEFAULT_ELEMENT_NAME);
    org.opensaml.xml.signature.X509Certificate cert =
        (org.opensaml.xml.signature.X509Certificate) buildXMLObject(org.opensaml.xml.signature.X509Certificate.DEFAULT_ELEMENT_NAME);
    String value =
        org.apache.xml.security.utils.Base64.encode(cred.getEntityCertificate().getEncoded());
    cert.setValue(value);
    data.getX509Certificates().add(cert);
    keyInfo.getX509Datas().add(data);
    signature.setKeyInfo(keyInfo);
    return signature;
  } catch (CertificateEncodingException e) {
    throw new SSOAgentException("Error getting certificate", e);
  }
}

代码示例来源:origin: org.apache.cxf.fediz/fediz-idp-core

@Override
public SecurityToken requestSecurityToken() throws Exception {
  KerberosSecurity bst = new KerberosSecurity(DOMUtils.createDocument());
  bst.setValueType(WSConstants.WSS_GSS_KRB_V5_AP_REQ);
  bst.setToken(token);
  bst.addWSUNamespace();
  bst.setID(WSSConfig.getNewInstance().getIdAllocator().createSecureId("BST-", bst));
  SecurityToken securityToken = new SecurityToken(bst.getID());
  securityToken.setToken(bst.getElement());
  securityToken.setWsuId(bst.getID());
  securityToken.setData(bst.getToken());
  String sha1 = Base64.encode(KeyUtils.generateDigest(bst.getToken()));
  securityToken.setSHA1(sha1);
  securityToken.setTokenType(bst.getValueType());
  return securityToken;
}

相关文章