Java Utililty Methods Security

List of utility methods to do Security

Description

The list of methods to do Security are organized into topic(s).

Method

booleanisDN(String user)
is DN
try {
    new X500Principal(user);
    return true;
} catch (Exception ex) {
    return false;
booleanisOriginalTicketGrantingTicket(KerberosTicket ticket)
Check whether the server principal is the TGS's principal
return isTicketGrantingServerPrincipal(ticket.getServer());
StringjaasConfig(String loginContextName, String key)
Construct a JAAS configuration object per kafka jaas configuration file
AppConfigurationEntry[] configurationEntries = Configuration.getConfiguration()
        .getAppConfigurationEntry(loginContextName);
if (configurationEntries == null) {
    String errorMessage = "Could not find a '" + loginContextName + "' entry in this configuration.";
    throw new IOException(errorMessage);
for (AppConfigurationEntry entry : configurationEntries) {
    Object val = entry.getOptions().get(key);
...
StringjaasConfigOption(Configuration jaasConfig, String loginContextName, String key, String loginModuleName)
Returns the configuration option for key from the login context loginContextName of the specified JAAS configuration.
AppConfigurationEntry[] configurationEntries = jaasConfig.getAppConfigurationEntry(loginContextName);
if (configurationEntries == null) {
    String errorMessage = "Could not find a '" + loginContextName + "' entry in this JAAS configuration.";
    throw new IOException(errorMessage);
for (AppConfigurationEntry entry : configurationEntries) {
    if (loginModuleName != null && !loginModuleName.equals(entry.getLoginModuleName()))
        continue;
...
LoginContextkinit(String username, char[] password)
kinit via code
CallbackHandler handler = (callbacks) -> {
    for (Callback c : callbacks) {
        if (c instanceof NameCallback) {
            ((NameCallback) c).setName(username);
        if (c instanceof PasswordCallback) {
            ((PasswordCallback) c).setPassword(password);
};
LoginContext lc = new LoginContext(LOGIN_MODULE, handler);
lc.login();
return lc;
voidlogout()
logout
if (loginContext != null) {
    loginContext.logout();
    loginContext = null;
} else {
    throw new LoginException("You must login before trying to log out!");
HashMapparseDirectives(byte[] buf)
Parses digest-challenge string, extracting each token and value(s)
HashMap<String, String> map = new HashMap<String, String>();
boolean gettingKey = true;
boolean gettingQuotedValue = false;
boolean expectSeparator = false;
byte bch;
ByteArrayOutputStream key = new ByteArrayOutputStream(10);
ByteArrayOutputStream value = new ByteArrayOutputStream(10);
int i = skipLws(buf, 0);
...
SecretKeysecretKey(final String key)
secret Key
return new SecretKey() {
    private static final long serialVersionUID = -829558999158937420L;
    public String getAlgorithm() {
        return ALGO;
    public byte[] getEncoded() {
        return key.getBytes();
    public String getFormat() {
        return "RAW";
    @Override
    public void destroy() throws DestroyFailedException {
    @Override
    public boolean isDestroyed() {
        return false;
};
byte[]serializeKerberosTicket(KerberosTicket tgt)
serialize Kerberos Ticket
ByteArrayOutputStream bao = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bao);
out.writeObject(tgt);
out.flush();
out.close();
return bao.toByteArray();
byte[]stringToByte_8859_1(String str, boolean useUTF8)
Used to convert username-value, passwd or realm to 8859_1 encoding if all chars in string are within the 8859_1 (Latin 1) encoding range.
char[] buffer = str.toCharArray();
try {
    if (useUTF8) {
        for (int i = 0; i < buffer.length; i++) {
            if (buffer[i] > '\u00FF') {
                return str.getBytes("UTF8");
    return str.getBytes("8859_1");
} catch (UnsupportedEncodingException e) {
    throw new SaslException("cannot encode string in UTF8 or 8859-1 (Latin-1)", e);