Computes the 24 byte ANSI password hash given the 8 byte server challenge.

@param tc
@param chlng
@return the hash for the given challenge
@throws GeneralSecurityException
12599
public byte[] getAnsiHash ( CIFSContext tc, byte[] chlng ) throws GeneralSecurityException {
        switch ( tc.getConfig().getLanManCompatibility() ) {
        case 0:
        case 1:
            return NtlmUtil.getPreNTLMResponse(tc, this.password, chlng);
        case 2:
            return NtlmUtil.getNTLMResponse(this.password, chlng);
        case 3:
        case 4:
        case 5:
            if ( this.clientChallenge == null ) {
                this.clientChallenge = new byte[8];
                tc.getConfig().getRandom().nextBytes(this.clientChallenge);
            }
            return NtlmUtil.getLMv2Response(this.domain, this.username, this.password, chlng, this.clientChallenge);
        default:
            return NtlmUtil.getPreNTLMResponse(tc, this.password, chlng);
        }
    }
644851
public final long hash(byte[] data, int c, int d) {
        return SipHasher.hash(
            c, d,
            this.v0,
            this.v1,
            this.v2,
            this.v3,
            data
        );
    }
317353
protected String computeSha1Signature(String baseString, @Sensitive String keyString) throws GeneralSecurityException, UnsupportedEncodingException {

        byte[] keyBytes = keyString.getBytes(CommonWebConstants.UTF_8);
        SecretKey secretKey = new SecretKeySpec(keyBytes, "HmacSHA1");

        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(secretKey);

        byte[] text = baseString.getBytes(CommonWebConstants.UTF_8);

        return new String(Base64.encodeBase64(mac.doFinal(text)), CommonWebConstants.UTF_8).trim();
    }
329067
function encryptAndHash (state, ciphertext, plaintext) {
  assert(state.byteLength === STATELEN)
  assert(ciphertext.byteLength != null)
  assert(plaintext.byteLength != null)

  var cstate = state.subarray(CIPHER_BEGIN, CIPHER_END)
  var h = state.subarray(HASH_BEGIN, HASH_END)

  cipherState.encryptWithAd(cstate, ciphertext, h, plaintext)
  encryptAndHash.bytesRead = cipherState.encryptWithAd.bytesRead
  encryptAndHash.bytesWritten = cipherState.encryptWithAd.bytesWritten
  mixHash(state, ciphertext.subarray(0, encryptAndHash.bytesWritten))
}
559030
public static byte[] hashPassword(String password) throws InvalidKeyException, NoSuchAlgorithmException {

        PasswordEncryptor encryptor = new PasswordEncryptor();
        return encryptor.generateEncryptedPassword(password, null, PasswordEncryptor.MD4, null, null);
    }
1196444
private static byte[] lmResponse(final byte[] hash, final byte[] challenge) throws AuthenticationException {
        try {
            final byte[] keyBytes = new byte[21];
            System.arraycopy(hash, 0, keyBytes, 0, 16);
            final Key lowKey = createDESKey(keyBytes, 0);
            final Key middleKey = createDESKey(keyBytes, 7);
            final Key highKey = createDESKey(keyBytes, 14);
            final Cipher des = Cipher.getInstance("DES/ECB/NoPadding");
            des.init(Cipher.ENCRYPT_MODE, lowKey);
            final byte[] lowResponse = des.doFinal(challenge);
            des.init(Cipher.ENCRYPT_MODE, middleKey);
            final byte[] middleResponse = des.doFinal(challenge);
            des.init(Cipher.ENCRYPT_MODE, highKey);
            final byte[] highResponse = des.doFinal(challenge);
            final byte[] lmResponse = new byte[24];
            System.arraycopy(lowResponse, 0, lmResponse, 0, 8);
            System.arraycopy(middleResponse, 0, lmResponse, 8, 8);
            System.arraycopy(highResponse, 0, lmResponse, 16, 8);
            return lmResponse;
        } catch (final Exception e) {
            throw new AuthenticationException(e.getMessage(), e);
        }
    }
440181
private static byte[] lmHash(final String password) throws AuthenticationException {
        try {
            final byte[] oemPassword = password.toUpperCase(Locale.ROOT).getBytes(StandardCharsets.US_ASCII);
            final int length = Math.min(oemPassword.length, 14);
            final byte[] keyBytes = new byte[14];
            System.arraycopy(oemPassword, 0, keyBytes, 0, length);
            final Key lowKey = createDESKey(keyBytes, 0);
            final Key highKey = createDESKey(keyBytes, 7);
            final byte[] magicConstant = "KGS!@#$%".getBytes(StandardCharsets.US_ASCII);
            final Cipher des = Cipher.getInstance("DES/ECB/NoPadding");
            des.init(Cipher.ENCRYPT_MODE, lowKey);
            final byte[] lowHash = des.doFinal(magicConstant);
            des.init(Cipher.ENCRYPT_MODE, highKey);
            final byte[] highHash = des.doFinal(magicConstant);
            final byte[] lmHash = new byte[16];
            System.arraycopy(lowHash, 0, lmHash, 0, 8);
            System.arraycopy(highHash, 0, lmHash, 8, 8);
            return lmHash;
        } catch (final Exception e) {
            throw new AuthenticationException(e.getMessage(), e);
        }
    }
771331
def encrypt_PBEWithSHAAndTwofishCBC(plaintext_data, password, salt, iteration_count):
    
    iv  = derive_key(hashlib.sha1, PURPOSE_IV_MATERIAL,  password, salt, iteration_count, 16)
    key = derive_key(hashlib.sha1, PURPOSE_KEY_MATERIAL, password, salt, iteration_count, 256//8)

    plaintext_data = add_pkcs7_padding(plaintext_data, 16)
    plaintext_data = bytearray(plaintext_data)
    plaintext_len = len(plaintext_data)
    assert plaintext_len % 16 == 0

    ciphertext = bytearray()

    from twofish import Twofish
    cipher = Twofish(key)

    last_cipher_block = bytearray(iv)
    for block_offset in range(0, plaintext_len, 16):
        plaintext_block = plaintext_data[block_offset:block_offset+16]
        cipher_block = bytearray(cipher.encrypt(bytes(xor_bytearrays(plaintext_block, last_cipher_block))))
        ciphertext.extend(cipher_block)
        last_cipher_block = cipher_block

    return bytes(ciphertext)
799635
static public byte[] getNTLMResponse ( String password, byte[] challenge ) throws GeneralSecurityException {
        byte[] p21 = new byte[21];
        byte[] p24 = new byte[24];

        byte[] uni = Strings.getUNIBytes(password);
        MessageDigest md4 = Crypto.getMD4();
        md4.update(uni);
        md4.digest(p21, 0, 16);
        NtlmUtil.E(p21, challenge, p24);
        return p24;
    }
287023
def sha2_crypt(key, salt, hashfunc, rounds=_ROUNDS_DEFAULT):
    
    key = key.encode('utf-8')
    h = hashfunc()
    alt_h = hashfunc()
    digest_size = h.digest_size
    key_len = len(key)

    # First, feed key, salt and then key again to the alt hash
    alt_h.update(key)
    alt_h.update(salt.encode('utf-8'))
    alt_h.update(key)
    alt_result = alt_h.digest()

    # Feed key and salt to the primary hash
    h.update(key)
    h.update(salt.encode('utf-8'))

    # Feed as many (loopping) bytes from alt digest as the length of the key
    for i in range(key_len//digest_size):
        h.update(alt_result)
    h.update(alt_result[:(key_len % digest_size)])

    # Take the binary representation of the length of the key and for every
    # 1 add the alternate digest, for every 0 the key
    bits = key_len
    while bits > 0:
        if bits & 1 == 0:
            h.update(key)
        else:
            h.update(alt_result)
        bits >>= 1

    # Store the results from the primary hash
    alt_result = h.digest()

    h = hashfunc()

    # Add password for each character in the password
    for i in range(key_len):
        h.update(key)

    temp_result = h.digest()

    # Compute a P array of the bytes in temp repeated for the length of the key
    p_bytes = temp_result * (key_len // digest_size)
    p_bytes += temp_result[:(key_len % digest_size)]

    alt_h = hashfunc()

    # Add the salt 16 + arbitrary amount decided by first byte in alt digest
    for i in range(16 + byte2int(alt_result[0])):
        alt_h.update(salt.encode('utf-8'))

    temp_result = alt_h.digest()

    # Compute a S array of the bytes in temp_result repeated for the length
    # of the salt
    s_bytes = temp_result * (len(salt) // digest_size)
    s_bytes += temp_result[:(len(salt) % digest_size)]

    # Do the actual iterations
    for i in range(rounds):
        h = hashfunc()

        # Alternate adding either the P array or the alt digest
        if i & 1 != 0:
            h.update(p_bytes)
        else:
            h.update(alt_result)

        # If the round is divisible by 3, add the S array
        if i % 3 != 0:
            h.update(s_bytes)

        # If the round is divisible by 7, add the P array
        if i % 7 != 0:
            h.update(p_bytes)

        # Alternate adding either the P array or the alt digest, opposite
        # of first step
        if i & 1 != 0:
            h.update(alt_result)
        else:
            h.update(p_bytes)

        alt_result = h.digest()

    # Compute the base64-ish representation of the hash
    ret = []
    if digest_size == 64:
        # SHA-512
        ret.append(b64_from_24bit(alt_result[0], alt_result[21], alt_result[42], 4))
        ret.append(b64_from_24bit(alt_result[22], alt_result[43], alt_result[1], 4))
        ret.append(b64_from_24bit(alt_result[44], alt_result[2], alt_result[23], 4))
        ret.append(b64_from_24bit(alt_result[3], alt_result[24], alt_result[45], 4))
        ret.append(b64_from_24bit(alt_result[25], alt_result[46], alt_result[4], 4))
        ret.append(b64_from_24bit(alt_result[47], alt_result[5], alt_result[26], 4))
        ret.append(b64_from_24bit(alt_result[6], alt_result[27], alt_result[48], 4))
        ret.append(b64_from_24bit(alt_result[28], alt_result[49], alt_result[7], 4))
        ret.append(b64_from_24bit(alt_result[50], alt_result[8], alt_result[29], 4))
        ret.append(b64_from_24bit(alt_result[9], alt_result[30], alt_result[51], 4))
        ret.append(b64_from_24bit(alt_result[31], alt_result[52], alt_result[10], 4))
        ret.append(b64_from_24bit(alt_result[53], alt_result[11], alt_result[32], 4))
        ret.append(b64_from_24bit(alt_result[12], alt_result[33], alt_result[54], 4))
        ret.append(b64_from_24bit(alt_result[34], alt_result[55], alt_result[13], 4))
        ret.append(b64_from_24bit(alt_result[56], alt_result[14], alt_result[35], 4))
        ret.append(b64_from_24bit(alt_result[15], alt_result[36], alt_result[57], 4))
        ret.append(b64_from_24bit(alt_result[37], alt_result[58], alt_result[16], 4))
        ret.append(b64_from_24bit(alt_result[59], alt_result[17], alt_result[38], 4))
        ret.append(b64_from_24bit(alt_result[18], alt_result[39], alt_result[60], 4))
        ret.append(b64_from_24bit(alt_result[40], alt_result[61], alt_result[19], 4))
        ret.append(b64_from_24bit(alt_result[62], alt_result[20], alt_result[41], 4))
        ret.append(b64_from_24bit(int2byte(0), int2byte(0), alt_result[63], 2))
    else:
        # SHA-256
        ret.append(b64_from_24bit(alt_result[0], alt_result[10], alt_result[20], 4))
        ret.append(b64_from_24bit(alt_result[21], alt_result[1], alt_result[11], 4))
        ret.append(b64_from_24bit(alt_result[12], alt_result[22], alt_result[2], 4))
        ret.append(b64_from_24bit(alt_result[3], alt_result[13], alt_result[23], 4))
        ret.append(b64_from_24bit(alt_result[24], alt_result[4], alt_result[14], 4))
        ret.append(b64_from_24bit(alt_result[15], alt_result[25], alt_result[5], 4))
        ret.append(b64_from_24bit(alt_result[6], alt_result[16], alt_result[26], 4))
        ret.append(b64_from_24bit(alt_result[27], alt_result[7], alt_result[17], 4))
        ret.append(b64_from_24bit(alt_result[18], alt_result[28], alt_result[8], 4))
        ret.append(b64_from_24bit(alt_result[9], alt_result[19], alt_result[29], 4))
        ret.append(b64_from_24bit(int2byte(0), alt_result[31], alt_result[30], 3))

    algo = 6 if digest_size == 64 else 5
    if rounds == _ROUNDS_DEFAULT:
        return '${0}${1}${2}'.format(algo, salt, ''.join(ret))
    else:
        return '${0}$rounds={1}${2}${3}'.format(algo, rounds, salt, ''.join(ret))
1314828
@Override
    public byte[] getUnicodeHash ( CIFSContext tc, byte[] chlng ) throws GeneralSecurityException {
        if ( this.hashesExternal ) {
            return this.unicodeHash;
        }
        return super.getUnicodeHash(tc, chlng);
    }
343180
def get_lmv2_response(domain, username, password, server_challenge, client_challenge):
        
        ntlmv2_hash = PasswordAuthentication.ntowfv2(domain, username, password.encode('utf-16le'))
        hmac_context = hmac.HMAC(ntlmv2_hash, hashes.MD5(), backend=default_backend())
        hmac_context.update(server_challenge)
        hmac_context.update(client_challenge)
        lmv2_hash = hmac_context.finalize()

        # The LMv2 master user session key is a HMAC MD5 of the NTLMv2 and LMv2 hash
        session_key = hmac.HMAC(ntlmv2_hash, hashes.MD5(), backend=default_backend())
        session_key.update(lmv2_hash)

        return lmv2_hash + client_challenge, session_key.finalize()
429824
protected String formatCredentials(final ChallengeResponse challenge) throws GeneralSecurityException
    {
        // Data buffer
        final StringBuffer sb = new StringBuffer();
        
        // Indexes buffer
        final StringBuffer isb = new StringBuffer();
        final String timeIssued = Long.toString(System.currentTimeMillis());
        int i = timeIssued.length();
        sb.append(timeIssued);
        
        isb.append(i);
        
        final String identifier = challenge.getIdentifier();
        sb.append('/');
        sb.append(identifier);
        
        i += identifier.length() + 1;
        isb.append(',').append(i);
        
        sb.append('/');
        sb.append(challenge.getSecret());
        
        // Store indexes at the end of the string
        sb.append('/');
        sb.append(isb);
        
        return Base64.encode(
                CryptoUtils.encrypt(this.getEncryptAlgorithm(), this.getEncryptSecretKey(), sb.toString()), false);
    }
1287252
private Cipher createCipher() throws GeneralSecurityException {
		if(algorithmProvider != null) {
			return Cipher.getInstance(algorithm, algorithmProvider);
		} else {
			return Cipher.getInstance(algorithm);
		}
	}
37180
public static byte[] getNTLMResponse(String password, byte[] challenge)
            throws Exception {
        byte[] ntlmHash = ntlmHash(password);
        return lmResponse(ntlmHash, challenge);
    }
418645
@Override
    public byte[] getUserSessionKey ( CIFSContext tc, byte[] chlng ) {
        if ( this.hashesExternal ) {
            return null;
        }
        return super.getUserSessionKey(tc, chlng);
    }
1289092
@Override
  public void getBytesToSendToPeer(ByteBuffer bytes) throws GeneralSecurityException {
    if (outputFrame == null) { // A null outputFrame indicates we haven't started the handshake.
      if (isClient) {
        outputFrame = handshaker.startClientHandshake();
      } else {
        // The server needs bytes to process before it can start the handshake.
        return;
      }
    }
    // Write as many bytes as we are able.
    ByteBuffer outputFrameAlias = outputFrame;
    if (outputFrame.remaining() > bytes.remaining()) {
      outputFrameAlias = outputFrame.duplicate();
      outputFrameAlias.limit(outputFrameAlias.position() + bytes.remaining());
    }
    bytes.put(outputFrameAlias);
    outputFrame.position(outputFrameAlias.position());
  }