Java Code Examples for org.apache.tomcat.util.buf.HexUtils#toHexString()

The following examples show how to use org.apache.tomcat.util.buf.HexUtils#toHexString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MessageDigestCredentialHandler.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
protected String mutate(String inputCredentials, byte[] salt, int iterations) {
    if (algorithm == null) {
        return inputCredentials;
    } else {
        byte[] userDigest;
        if (salt == null) {
            userDigest = ConcurrentMessageDigest.digest(algorithm, iterations,
                    inputCredentials.getBytes(encoding));
        } else {
            userDigest = ConcurrentMessageDigest.digest(algorithm, iterations,
                    salt, inputCredentials.getBytes(encoding));
        }
        return HexUtils.toHexString(userDigest);
    }
}
 
Example 2
Source File: RealmBase.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Digest password using the algorithm specified and convert the result to a
 * corresponding hex string.
 *
 * @param credentials Password or other credentials to use in authenticating
 *                    this username
 * @param algorithm   Algorithm used to do the digest
 * @param encoding    Character encoding of the string to digest
 *
 * @return The digested credentials as a hex string or the original plain
 *         text credentials if an error occurs.
 *
 * @deprecated  Unused. This will be removed in Tomcat 9.
 */
@Deprecated
public static final String Digest(String credentials, String algorithm,
                                  String encoding) {

    try {
        // Obtain a new message digest with "digest" encryption
        MessageDigest md =
            (MessageDigest) MessageDigest.getInstance(algorithm).clone();

        // encode the credentials
        // Should use the digestEncoding, but that's not a static field
        if (encoding == null) {
            md.update(credentials.getBytes());
        } else {
            md.update(credentials.getBytes(encoding));
        }

        // Digest the credentials and return as hexadecimal
        return (HexUtils.toHexString(md.digest()));
    } catch(Exception ex) {
        log.error(ex);
        return credentials;
    }

}
 
Example 3
Source File: RealmBase.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Digest password using the algorithm specified and
 * convert the result to a corresponding hex string.
 * If exception, the plain credentials string is returned
 *
 * @param credentials Password or other credentials to use in
 *  authenticating this username
 * @param algorithm Algorithm used to do the digest
 * @param encoding Character encoding of the string to digest
 */
public static final String Digest(String credentials, String algorithm,
                                  String encoding) {

    try {
        // Obtain a new message digest with "digest" encryption
        MessageDigest md =
            (MessageDigest) MessageDigest.getInstance(algorithm).clone();

        // encode the credentials
        // Should use the digestEncoding, but that's not a static field
        if (encoding == null) {
            md.update(credentials.getBytes());
        } else {
            md.update(credentials.getBytes(encoding));                
        }

        // Digest the credentials and return as hexadecimal
        return (HexUtils.toHexString(md.digest()));
    } catch(Exception ex) {
        log.error(ex);
        return credentials;
    }

}
 
Example 4
Source File: RealmBase.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Digest password using the algorithm specified and
 * convert the result to a corresponding hex string.
 * If exception, the plain credentials string is returned
 *
 * @param credentials Password or other credentials to use in
 *  authenticating this username
 * @param algorithm Algorithm used to do the digest
 * @param encoding Character encoding of the string to digest
 */
public static final String Digest(String credentials, String algorithm,
                                  String encoding) {

    try {
        // Obtain a new message digest with "digest" encryption
        MessageDigest md =
            (MessageDigest) MessageDigest.getInstance(algorithm).clone();

        // encode the credentials
        // Should use the digestEncoding, but that's not a static field
        if (encoding == null) {
            md.update(credentials.getBytes());
        } else {
            md.update(credentials.getBytes(encoding));
        }

        // Digest the credentials and return as hexadecimal
        return (HexUtils.toHexString(md.digest()));
    } catch(Exception ex) {
        log.error(ex);
        return credentials;
    }

}
 
Example 5
Source File: SecretKeyCredentialHandler.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
protected String mutate(String inputCredentials, byte[] salt, int iterations, int keyLength) {
    try {
        KeySpec spec = new PBEKeySpec(inputCredentials.toCharArray(), salt, iterations, keyLength);
        return HexUtils.toHexString(secretKeyFactory.generateSecret(spec).getEncoded());
    } catch (InvalidKeySpecException | IllegalArgumentException e) {
        log.warn(sm.getString("pbeCredentialHandler.invalidKeySpec"), e);
        return null;
    }
}
 
Example 6
Source File: AesEncryptProvider.java    From mPaaS with Apache License 2.0 5 votes vote down vote up
/**
 * 加密
 *
 * @param encryptStr
 * @return
 */
@Override
public String encrypt(String encryptStr) {
    synchronized (this.encryptor) {
        try {
            encryptor.init(Cipher.ENCRYPT_MODE, secretKey, ivParam);
            byte[] encodes = Base64.getEncoder().encode(encryptor.doFinal(encryptStr.getBytes(CHARSET_DEFAULT)));
            return HexUtils.toHexString(encodes);
        } catch (Exception e) {
            log.error(ENCRYPT_AES + "加密出错", e);
        }
    }
    return null;
}
 
Example 7
Source File: DesEncryptProvider.java    From mPaaS with Apache License 2.0 5 votes vote down vote up
/**
 * 加密
 *
 * @param encryptStr
 * @return
 */
@Override
public String encrypt(String encryptStr) {
    synchronized (this.encryptor) {
        try {
            encryptor.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] encodes = Base64.getEncoder().encode(encryptor.doFinal(encryptStr.getBytes(CHARSET_DEFAULT)));
            return HexUtils.toHexString(encodes);
        } catch (Exception e) {
            log.error(ENCRYPT_DES + "加密出错", e);
        }
    }
    return null;
}
 
Example 8
Source File: AesEncryptProvider.java    From mPass with Apache License 2.0 5 votes vote down vote up
/**
 * 加密
 *
 * @param encryptStr
 * @return
 */
@Override
public String encrypt(String encryptStr) {
    synchronized (this.encryptor) {
        try {
            encryptor.init(Cipher.ENCRYPT_MODE, secretKey, ivParam);
            byte[] encodes = Base64.getEncoder().encode(encryptor.doFinal(encryptStr.getBytes(CHARSET_DEFAULT)));
            return HexUtils.toHexString(encodes);
        } catch (Exception e) {
            log.error(ENCRYPT_AES + "加密出错", e);
        }
    }
    return null;
}
 
Example 9
Source File: DesEncryptProvider.java    From mPass with Apache License 2.0 5 votes vote down vote up
/**
 * 加密
 *
 * @param encryptStr
 * @return
 */
@Override
public String encrypt(String encryptStr) {
    synchronized (this.encryptor) {
        try {
            encryptor.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] encodes = Base64.getEncoder().encode(encryptor.doFinal(encryptStr.getBytes(CHARSET_DEFAULT)));
            return HexUtils.toHexString(encodes);
        } catch (Exception e) {
            log.error(ENCRYPT_DES + "加密出错", e);
        }
    }
    return null;
}
 
Example 10
Source File: RealmBase.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Digest the password using the specified algorithm and
 * convert the result to a corresponding hexadecimal string.
 * If exception, the plain credentials string is returned.
 *
 * @param credentials Password or other credentials to use in
 *  authenticating this username
 */
protected String digest(String credentials)  {

    // If no MessageDigest instance is specified, return unchanged
    if (hasMessageDigest() == false)
        return (credentials);

    // Digest the user credentials and return as hexadecimal
    synchronized (this) {
        try {
            md.reset();

            byte[] bytes = null;
            try {
                bytes = credentials.getBytes(getDigestCharset());
            } catch (UnsupportedEncodingException uee) {
                log.error("Illegal digestEncoding: " + getDigestEncoding(), uee);
                throw new IllegalArgumentException(uee.getMessage());
            }
            md.update(bytes);

            return (HexUtils.toHexString(md.digest()));
        } catch (Exception e) {
            log.error(sm.getString("realmBase.digest"), e);
            return (credentials);
        }
    }

}
 
Example 11
Source File: RealmBase.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Digest the password using the specified algorithm and
 * convert the result to a corresponding hexadecimal string.
 * If exception, the plain credentials string is returned.
 *
 * @param credentials Password or other credentials to use in
 *  authenticating this username
 */
protected String digest(String credentials)  {

    // If no MessageDigest instance is specified, return unchanged
    if (hasMessageDigest() == false)
        return (credentials);

    // Digest the user credentials and return as hexadecimal
    synchronized (this) {
        try {
            md.reset();

            byte[] bytes = null;
            try {
                bytes = credentials.getBytes(getDigestCharset());
            } catch (UnsupportedEncodingException uee) {
                log.error("Illegal digestEncoding: " + getDigestEncoding(), uee);
                throw new IllegalArgumentException(uee.getMessage());
            }
            md.update(bytes);

            return (HexUtils.toHexString(md.digest()));
        } catch (Exception e) {
            log.error(sm.getString("realmBase.digest"), e);
            return (credentials);
        }
    }

}
 
Example 12
Source File: BytesToHexDeserializer.java    From kafka-webview with MIT License 4 votes vote down vote up
@Override
public String deserialize(final String topic, final byte[] data) {
    // Convert bytes into Hex.
    return HexUtils.toHexString(data);
}
 
Example 13
Source File: BaseOpenIDConnectAuthenticator.java    From tomcat-oidcauth with Apache License 2.0 4 votes vote down vote up
/**
 * Add request attributes for the login or the login error page.
 *
 * @param request The request.
 *
 * @throws IOException If an I/O error happens.
 */
protected void addLoginConfiguration(final Request request)
	throws IOException {

	// generate state value and save it in the session
	final byte[] stateBytes = new byte[16];
	this.rand.nextBytes(stateBytes);
	final String state = HexUtils.toHexString(stateBytes);
	request.getSessionInternal(true).setNote(SESS_STATE_NOTE, state);

	// add OP authorization endpoints to the request for the login page
	final List<AuthEndpointDesc> authEndpoints = new ArrayList<>();
	final StringBuilder buf = new StringBuilder(128);
	for (int i = 0; i < this.opDescs.size(); i++) {
		final OPDescriptor opDesc = this.opDescs.get(i);

		// get the OP configuration
		final String issuer = opDesc.getIssuer();
		final OPConfiguration opConfig =
			this.ops.getOPConfiguration(issuer);

		// construct the authorization endpoint URL
		buf.setLength(0);
		buf.append(opConfig.getAuthorizationEndpoint());
		buf.append("?scope=openid");
		final String extraScopes = opDesc.getAdditionalScopes();
		if (extraScopes != null)
			buf.append(URLEncoder.encode(" " + extraScopes, UTF8.name()));
		buf.append("&response_type=code");
		buf.append("&client_id=").append(URLEncoder.encode(
				opDesc.getClientId(), UTF8.name()));
		buf.append("&redirect_uri=").append(URLEncoder.encode(
				this.getBaseURL(request) + Constants.FORM_ACTION,
				UTF8.name()));
		buf.append("&state=").append(i).append('Z').append(state);
		final String addlParams = opDesc.getExtraAuthEndpointParams();
		if (addlParams != null)
			buf.append('&').append(addlParams);

		// add the URL to the map
		authEndpoints.add(new AuthEndpointDesc(
				opDesc.getName(), issuer, buf.toString()));
	}
	request.setAttribute(AUTHEPS_ATT, authEndpoints);

	// add no form flag to the request
	request.setAttribute(NOFORM_ATT, Boolean.valueOf(this.noForm));
}