Java Code Examples for org.apache.commons.httpclient.util.EncodingUtil#getBytes()

The following examples show how to use org.apache.commons.httpclient.util.EncodingUtil#getBytes() . 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: StringPart.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Gets the content in bytes.  Bytes are lazily created to allow the charset to be changed
 * after the part is created.
 * 
 * @return the content in bytes
 */
private byte[] getContent() {
    if (content == null) {
        content = EncodingUtil.getBytes(value, getCharSet());
    }
    return content;
}
 
Example 2
Source File: NTLM.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Creates the first message (type 1 message) in the NTLM authentication sequence.
 * This message includes the user name, domain and host for the authentication session.
 *
 * @param host the computer name of the host requesting authentication.
 * @param domain The domain to authenticate with.
 * @return String the message to add to the HTTP request header.
 */
public String getType1Message(String host, String domain) {
    host = host.toUpperCase();
    domain = domain.toUpperCase();
    byte[] hostBytes = EncodingUtil.getBytes(host, DEFAULT_CHARSET);
    byte[] domainBytes = EncodingUtil.getBytes(domain, DEFAULT_CHARSET);

    int finalLength = 32 + hostBytes.length + domainBytes.length;
    prepareResponse(finalLength);
    
    // The initial id string.
    byte[] protocol = EncodingUtil.getBytes("NTLMSSP", DEFAULT_CHARSET);
    addBytes(protocol);
    addByte((byte) 0);

    // Type
    addByte((byte) 1);
    addByte((byte) 0);
    addByte((byte) 0);
    addByte((byte) 0);

    // Flags
    addByte((byte) 6);
    addByte((byte) 82);
    addByte((byte) 0);
    addByte((byte) 0);

    // Domain length (first time).
    int iDomLen = domainBytes.length;
    byte[] domLen = convertShort(iDomLen);
    addByte(domLen[0]);
    addByte(domLen[1]);

    // Domain length (second time).
    addByte(domLen[0]);
    addByte(domLen[1]);

    // Domain offset.
    byte[] domOff = convertShort(hostBytes.length + 32);
    addByte(domOff[0]);
    addByte(domOff[1]);
    addByte((byte) 0);
    addByte((byte) 0);

    // Host length (first time).
    byte[] hostLen = convertShort(hostBytes.length);
    addByte(hostLen[0]);
    addByte(hostLen[1]);

    // Host length (second time).
    addByte(hostLen[0]);
    addByte(hostLen[1]);

    // Host offset (always 32).
    byte[] hostOff = convertShort(32);
    addByte(hostOff[0]);
    addByte(hostOff[1]);
    addByte((byte) 0);
    addByte((byte) 0);

    // Host String.
    addBytes(hostBytes);

    // Domain String.
    addBytes(domainBytes);

    return getResponse();
}
 
Example 3
Source File: NTLM.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/** 
 * Creates the LANManager and NT response for the given password using the
 * given nonce.
 * @param password the password to create a hash for.
 * @param nonce the nonce sent by the server.
 * @return The response.
 * @throws HttpException If {@link #encrypt(byte[],byte[])} fails.
 */
private byte[] hashPassword(String password, byte[] nonce)
    throws AuthenticationException {
    byte[] passw = EncodingUtil.getBytes(password.toUpperCase(), credentialCharset);
    byte[] lmPw1 = new byte[7];
    byte[] lmPw2 = new byte[7];

    int len = passw.length;
    if (len > 7) {
        len = 7;
    }

    int idx;
    for (idx = 0; idx < len; idx++) {
        lmPw1[idx] = passw[idx];
    }
    for (; idx < 7; idx++) {
        lmPw1[idx] = (byte) 0;
    }

    len = passw.length;
    if (len > 14) {
        len = 14;
    }
    for (idx = 7; idx < len; idx++) {
        lmPw2[idx - 7] = passw[idx];
    }
    for (; idx < 14; idx++) {
        lmPw2[idx - 7] = (byte) 0;
    }

    // Create LanManager hashed Password
    byte[] magic = {
        (byte) 0x4B, (byte) 0x47, (byte) 0x53, (byte) 0x21, 
        (byte) 0x40, (byte) 0x23, (byte) 0x24, (byte) 0x25
    };

    byte[] lmHpw1;
    lmHpw1 = encrypt(lmPw1, magic);

    byte[] lmHpw2 = encrypt(lmPw2, magic);

    byte[] lmHpw = new byte[21];
    for (int i = 0; i < lmHpw1.length; i++) {
        lmHpw[i] = lmHpw1[i];
    }
    for (int i = 0; i < lmHpw2.length; i++) {
        lmHpw[i + 8] = lmHpw2[i];
    }
    for (int i = 0; i < 5; i++) {
        lmHpw[i + 16] = (byte) 0;
    }

    // Create the responses.
    byte[] lmResp = new byte[24];
    calcResp(lmHpw, nonce, lmResp);

    return lmResp;
}