use of org.apache.commons.codec.binary.Base64.encodeBase64String in project wso2-synapse by wso2.
the class PKCS8KeyStoreLoader method createPrivateKey.
/**
* Takes the (unencrypted) RSA private key in pkcs8 format, and creates a private key out of it
*
* @param keyBytes Byte Array of the private key
* @return PKCS8Encoded PrivateKey
*/
private PrivateKey createPrivateKey(byte[] keyBytes) {
int dataStart = HEADER.length();
int dataEnd = keyBytes.length - FOOTER.length() - 1;
int dataLength = dataEnd - dataStart;
byte[] keyContent = new byte[dataLength];
System.arraycopy(keyBytes, dataStart, keyContent, 0, dataLength);
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(new Base64().decode(keyContent));
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePrivate(pkcs8EncodedKeySpec);
} catch (NoSuchAlgorithmException e) {
handleException("Error getting a KeyFactory instance", e);
} catch (InvalidKeySpecException e) {
handleException("Error generating a private key", e);
}
return null;
}
use of org.apache.commons.codec.binary.Base64.encodeBase64String in project hadoop by apache.
the class RMWebServices method createContainerLaunchContext.
/**
* Create the ContainerLaunchContext required for the
* ApplicationSubmissionContext. This function takes the user information and
* generates the ByteBuffer structures required by the ContainerLaunchContext
*
* @param newApp
* the information provided by the user
* @return created context
* @throws BadRequestException
* @throws IOException
*/
protected ContainerLaunchContext createContainerLaunchContext(ApplicationSubmissionContextInfo newApp) throws BadRequestException, IOException {
// create container launch context
HashMap<String, ByteBuffer> hmap = new HashMap<String, ByteBuffer>();
for (Map.Entry<String, String> entry : newApp.getContainerLaunchContextInfo().getAuxillaryServiceData().entrySet()) {
if (entry.getValue().isEmpty() == false) {
Base64 decoder = new Base64(0, null, true);
byte[] data = decoder.decode(entry.getValue());
hmap.put(entry.getKey(), ByteBuffer.wrap(data));
}
}
HashMap<String, LocalResource> hlr = new HashMap<String, LocalResource>();
for (Map.Entry<String, LocalResourceInfo> entry : newApp.getContainerLaunchContextInfo().getResources().entrySet()) {
LocalResourceInfo l = entry.getValue();
LocalResource lr = LocalResource.newInstance(URL.fromURI(l.getUrl()), l.getType(), l.getVisibility(), l.getSize(), l.getTimestamp());
hlr.put(entry.getKey(), lr);
}
DataOutputBuffer out = new DataOutputBuffer();
Credentials cs = createCredentials(newApp.getContainerLaunchContextInfo().getCredentials());
cs.writeTokenStorageToStream(out);
ByteBuffer tokens = ByteBuffer.wrap(out.getData());
ContainerLaunchContext ctx = ContainerLaunchContext.newInstance(hlr, newApp.getContainerLaunchContextInfo().getEnvironment(), newApp.getContainerLaunchContextInfo().getCommands(), hmap, tokens, newApp.getContainerLaunchContextInfo().getAcls());
return ctx;
}
use of org.apache.commons.codec.binary.Base64.encodeBase64String in project amos-ss17-alexa by c-i-ber.
the class SignedRequestsHelper method hmac.
/**
* Compute the HMAC.
*
* @param stringToSign String to compute the HMAC over.
* @return base64-encoded hmac value.
*/
private String hmac(String stringToSign) {
String signature = null;
byte[] data;
byte[] rawHmac;
try {
data = stringToSign.getBytes(UTF8_CHARSET);
rawHmac = mac.doFinal(data);
Base64 encoder = new Base64();
signature = new String(encoder.encode(rawHmac));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(UTF8_CHARSET + " is unsupported!", e);
}
return signature;
}
use of org.apache.commons.codec.binary.Base64.encodeBase64String in project leopard by tanhaichao.
the class tls_sigature method CheckTLSSignatureEx.
public static CheckTLSSignatureResult CheckTLSSignatureEx(String urlSig, long sdkAppid, String identifier, String publicKey) throws DataFormatException {
CheckTLSSignatureResult result = new CheckTLSSignatureResult();
Security.addProvider(new BouncyCastleProvider());
// DeBaseUrl64 urlSig to json
Base64 decoder = new Base64();
byte[] compressBytes = base64_url.base64DecodeUrl(urlSig.getBytes(Charset.forName("UTF-8")));
// Decompression
Inflater decompression = new Inflater();
decompression.setInput(compressBytes, 0, compressBytes.length);
byte[] decompressBytes = new byte[1024];
int decompressLength = decompression.inflate(decompressBytes);
decompression.end();
String jsonString = new String(Arrays.copyOfRange(decompressBytes, 0, decompressLength));
// Get TLS.Sig from json
JSONObject jsonObject = new JSONObject(jsonString);
String sigTLS = jsonObject.getString("TLS.sig");
// debase64 TLS.Sig to get serailString
byte[] signatureBytes = decoder.decode(sigTLS.getBytes(Charset.forName("UTF-8")));
try {
String strSdkAppid = jsonObject.getString("TLS.sdk_appid");
String sigTime = jsonObject.getString("TLS.time");
String sigExpire = jsonObject.getString("TLS.expire_after");
if (Integer.parseInt(strSdkAppid) != sdkAppid) {
result.errMessage = new String("sdkappid " + strSdkAppid + " in tls sig not equal sdkappid " + sdkAppid + " in request");
return result;
}
if (System.currentTimeMillis() / 1000 - Long.parseLong(sigTime) > Long.parseLong(sigExpire)) {
result.errMessage = new String("TLS sig is out of date");
return result;
}
// Get Serial String from json
String SerialString = "TLS.appid_at_3rd:" + 0 + "\n" + "TLS.account_type:" + 0 + "\n" + "TLS.identifier:" + identifier + "\n" + "TLS.sdk_appid:" + sdkAppid + "\n" + "TLS.time:" + sigTime + "\n" + "TLS.expire_after:" + sigExpire + "\n";
Reader reader = new CharArrayReader(publicKey.toCharArray());
PEMParser parser = new PEMParser(reader);
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
Object obj = parser.readObject();
parser.close();
PublicKey pubKeyStruct = converter.getPublicKey((SubjectPublicKeyInfo) obj);
Signature signature = Signature.getInstance("SHA256withECDSA", "BC");
signature.initVerify(pubKeyStruct);
signature.update(SerialString.getBytes(Charset.forName("UTF-8")));
boolean bool = signature.verify(signatureBytes);
result.expireTime = Integer.parseInt(sigExpire);
result.initTime = Integer.parseInt(sigTime);
result.verifyResult = bool;
} catch (Exception e) {
e.printStackTrace();
result.errMessage = "Failed in checking sig";
}
return result;
}
use of org.apache.commons.codec.binary.Base64.encodeBase64String in project xian by happyyangyuan.
the class Authenticator method getBasicAuthorizationClientCredentials.
public static String[] getBasicAuthorizationClientCredentials(HttpRequest req) {
// extract Basic Authorization header
String authHeader = req.headers().get(HttpHeaderNames.AUTHORIZATION);
String[] clientCredentials = new String[2];
if (authHeader != null && authHeader.contains(BASIC)) {
String value = authHeader.replace(BASIC, "");
Base64 decoder = new Base64();
byte[] decodedBytes = decoder.decode(value);
String decoded = new String(decodedBytes, Charset.forName("UTF-8"));
// client_id:client_secret - should be changed by client password
String[] str = decoded.split(":");
if (str.length == 2) {
clientCredentials[0] = str[0];
clientCredentials[1] = str[1];
}
}
return clientCredentials;
}
Aggregations