private byte[] md5FromRecycledStringBuilder(StringBuilder sb, MessageDigest md) { md.update(StringUtils.charSequence2ByteBuffer(sb, ISO_8859_1)); sb.setLength(0); return md.digest(); }
public static byte[] charSequence2Bytes(CharSequence sb, Charset charset) { ByteBuffer bb = charSequence2ByteBuffer(sb, charset); return byteBuffer2ByteArray(bb); }
private byte[] digest(ConsumerKey consumerAuth, RequestToken userAuth, ByteBuffer message) throws InvalidKeyException { StringBuilder sb = StringBuilderPool.DEFAULT.stringBuilder(); Utf8UrlEncoder.encodeAndAppendQueryElement(sb, consumerAuth.getSecret()); sb.append('&'); if (userAuth != null && userAuth.getSecret() != null) { Utf8UrlEncoder.encodeAndAppendQueryElement(sb, userAuth.getSecret()); } byte[] keyBytes = StringUtils.charSequence2Bytes(sb, UTF_8); SecretKeySpec signingKey = new SecretKeySpec(keyBytes, HMAC_SHA1_ALGORITHM); mac.init(signingKey); mac.reset(); mac.update(message); return mac.doFinal(); }
private void newResponse(MessageDigest md) { // when using preemptive auth, the request uri is missing if (uri != null) { // BEWARE: compute first as it uses the cached StringBuilder String digestUri = AuthenticatorUtils.computeRealmURI(uri, useAbsoluteURI, omitQuery); StringBuilder sb = StringBuilderPool.DEFAULT.stringBuilder(); // WARNING: DON'T MOVE, BUFFER IS RECYCLED!!!! byte[] ha1 = ha1(sb, md); byte[] ha2 = ha2(sb, digestUri, md); appendBase16(sb, ha1); appendMiddlePart(sb); appendBase16(sb, ha2); byte[] responseDigest = md5FromRecycledStringBuilder(sb, md); response = toHexString(responseDigest); } }
private static String toHexString(byte[] data) { StringBuilder buffer = StringUtils.stringBuilder(); for (int i = 0; i < data.length; i++) { buffer.append(Integer.toHexString((data[i] & 0xf0) >>> 4)); buffer.append(Integer.toHexString(data[i] & 0x0f)); } return buffer.toString(); }
public ThreadSafeHMAC(ConsumerKey consumerAuth, RequestToken userAuth) { StringBuilder sb = StringUtils.stringBuilder(); Utf8UrlEncoder.encodeAndAppendQueryElement(sb, consumerAuth.getSecret()); sb.append('&'); if(userAuth != null && userAuth.getSecret() != null) { Utf8UrlEncoder.encodeAndAppendQueryElement(sb, userAuth.getSecret()); } byte[] keyBytes = StringUtils.charSequence2Bytes(sb, UTF_8); SecretKeySpec signingKey = new SecretKeySpec(keyBytes, HMAC_SHA1_ALGORITHM); // Get an hmac_sha1 instance and initialize with the signing key try { mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); } catch (Exception e) { throw new IllegalArgumentException(e); } }
private void newCnonce(MessageDigest md) { byte[] b = new byte[8]; ThreadLocalRandom.current().nextBytes(b); b = md.digest(b); cnonce = toHexString(b); }
private byte[] ha1(StringBuilder sb, MessageDigest md) { // if algorithm is "MD5" or is unspecified => A1 = username ":" realm-value ":" // passwd // if algorithm is "MD5-sess" => A1 = MD5( username-value ":" realm-value ":" // passwd ) ":" nonce-value ":" cnonce-value sb.append(principal).append(':').append(realmName).append(':').append(password); byte[] core = md5FromRecycledStringBuilder(sb, md); if (algorithm == null || algorithm.equals("MD5")) { // A1 = username ":" realm-value ":" passwd return core; } else if ("MD5-sess".equals(algorithm)) { // A1 = MD5(username ":" realm-value ":" passwd ) ":" nonce ":" cnonce appendBase16(sb, core); sb.append(':').append(nonce).append(':').append(cnonce); return md5FromRecycledStringBuilder(sb, md); } throw new UnsupportedOperationException("Digest algorithm not supported: " + algorithm); }
private static String computeContentType(String base, byte[] multipartBoundary) { StringBuilder buffer = StringUtils.stringBuilder().append(base); if (!base.endsWith(";")) buffer.append(';'); return buffer.append(" boundary=").append(new String(multipartBoundary, US_ASCII)).toString(); }
private String getMd5(String what) throws Exception { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(what.getBytes(StandardCharsets.ISO_8859_1)); byte[] hash = md.digest(); return StringUtils.toHexString(hash); } }
public static ByteBuffer urlEncodeFormParams(List<Param> params, Charset charset) { return StringUtils.charSequence2ByteBuffer(urlEncodeFormParams0(params, charset), US_ASCII); }
public static byte[] charSequence2Bytes(CharSequence sb, Charset charset) { ByteBuffer bb = charSequence2ByteBuffer(sb, charset); return byteBuffer2ByteArray(bb); } }
protected String withQueryWithoutParams(final String query) { // encode query StringBuilder sb = StringUtils.stringBuilder(); encodeAndAppendQuery(sb, query); return sb.toString(); }
private static String computeDigestAuthentication(Realm realm) { String realmUri = computeRealmURI(realm.getUri(), realm.isUseAbsoluteURI(), realm.isOmitQuery()); StringBuilder builder = new StringBuilder().append("Digest "); append(builder, "username", realm.getPrincipal(), true); append(builder, "realm", realm.getRealmName(), true); append(builder, "nonce", realm.getNonce(), true); append(builder, "uri", realmUri, true); if (isNonEmpty(realm.getAlgorithm())) append(builder, "algorithm", realm.getAlgorithm(), false); append(builder, "response", realm.getResponse(), true); if (realm.getOpaque() != null) append(builder, "opaque", realm.getOpaque(), true); if (realm.getQop() != null) { append(builder, "qop", realm.getQop(), false); // nc and cnonce only sent if server sent qop append(builder, "nc", realm.getNc(), false); append(builder, "cnonce", realm.getCnonce(), true); } builder.setLength(builder.length() - 2); // remove tailing ", " // FIXME isn't there a more efficient way? return new String(StringUtils.charSequence2Bytes(builder, ISO_8859_1)); }
String computeSignature(ConsumerKey consumerAuth, RequestToken userAuth, Uri uri, String method, List<Param> formParams, List<Param> queryParams, long oauthTimestamp, String percentEncodedNonce) throws InvalidKeyException { StringBuilder sb = signatureBaseString( consumerAuth, userAuth, uri, method, formParams, queryParams, oauthTimestamp, percentEncodedNonce); ByteBuffer rawBase = StringUtils.charSequence2ByteBuffer(sb, UTF_8); byte[] rawSignature = digest(consumerAuth, userAuth, rawBase); // and finally, base64 encoded... phew! return Base64.getEncoder().encodeToString(rawSignature); }
protected String withoutQueryWithParams(final List<Param> queryParams) { // concatenate encoded query params StringBuilder sb = StringUtils.stringBuilder(); encodeAndAppendQueryParams(sb, queryParams); sb.setLength(sb.length() - 1); return sb.toString(); } }, //
private static String computeDigestAuthentication(Realm realm) { StringBuilder builder = new StringBuilder().append("Digest "); append(builder, "username", realm.getPrincipal(), true); append(builder, "realm", realm.getRealmName(), true); append(builder, "nonce", realm.getNonce(), true); append(builder, "uri", computeRealmURI(realm), true); if (isNonEmpty(realm.getAlgorithm())) append(builder, "algorithm", realm.getAlgorithm(), false); append(builder, "response", realm.getResponse(), true); if (realm.getOpaque() != null) append(builder, "opaque", realm.getOpaque(), true); if (realm.getQop() != null) { append(builder, "qop", realm.getQop(), false); // nc and cnonce only sent if server sent qop append(builder, "nc", realm.getNc(), false); append(builder, "cnonce", realm.getCnonce(), true); } builder.setLength(builder.length() - 2); // remove tailing ", " // FIXME isn't there a more efficient way? return new String(StringUtils.charSequence2Bytes(builder, ISO_8859_1)); }
nettyBody = new NettyByteBufferBody(StringUtils.charSequence2ByteBuffer(request.getStringData(), bodyCharset));
protected String withQueryWithParams(final String query, final List<Param> queryParams) { // concatenate raw query + raw query params StringBuilder sb = StringUtils.stringBuilder(); sb.append(query); appendRawQueryParams(sb, queryParams); sb.setLength(sb.length() - 1); return sb.toString(); }
private byte[] md5FromRecycledStringBuilder(StringBuilder sb, MessageDigest md) { md.update(StringUtils.charSequence2ByteBuffer(sb, ISO_8859_1)); sb.setLength(0); return md.digest(); }