Serialize a challenge into it's binary representation

@param challenge   the challenge to serialize
@param hmacSecret the secret used to generate the HMAC field
@return an array of bytes representing the provided Challenge
979142
public static byte[] serialize(Challenge challenge, byte[] hmacSecret) {
    MiniMessagePack.Packer packer = new MiniMessagePack.Packer();
    packer.pack(VERSION);
    packer.pack(CHALLENGE_MAGIC);
    packer.pack(challenge.getUniqueData());
    packer.pack(challenge.getValidFromTimestamp());
    packer.pack(challenge.getValidToTimestamp());
    packer.pack(challenge.getFingerprint().getBytes());
    packer.pack(challenge.getServerName());
    packer.pack(challenge.getUserName());
    byte[] bytes = packer.getBytes();
    byte[] mac = getAuthenticationCode(hmacSecret, bytes);
    packer.pack(mac);
    return packer.getBytes();
  }
37180
public static byte[] getNTLMResponse(String password, byte[] challenge)
            throws Exception {
        byte[] ntlmHash = ntlmHash(password);
        return lmResponse(ntlmHash, challenge);
    }
676580
private String getSignature(String sharedSecret, Map<String, String> params) {
        StringBuffer buffer = new StringBuffer();
        buffer.append(sharedSecret);
        for (Map.Entry<String, String> entry : params.entrySet()) {
            buffer.append(entry.getKey());
            buffer.append(entry.getValue());
        }

        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            return ByteUtilities.toHexString(md.digest(buffer.toString().getBytes("UTF-8")));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException u) {
            throw new RuntimeException(u);
        }
    }
696655
private byte[] decryptV2Data(AES256v2Ciphertext aesCiphertext,
      SecretKey decryptionKey, SecretKey hmacKey) throws CryptorException {

    try {
      Mac mac = Mac.getInstance(HMAC_ALGORITHM);
      mac.init(hmacKey);
      byte[] hmacValue = mac.doFinal(aesCiphertext.getDataToHMAC());

      if (!arraysEqual(hmacValue, aesCiphertext.getHmac())) {
        throw new InvalidHMACException("Incorrect HMAC value.");
      }

      Cipher cipher = Cipher.getInstance(AES_CIPHER_ALGORITHM);
      cipher.init(Cipher.DECRYPT_MODE, decryptionKey, new IvParameterSpec(
          aesCiphertext.getIv()));

      return cipher.doFinal(aesCiphertext.getCiphertext());
    } catch (InvalidKeyException e) {
      throw new CryptorException(
          "Caught InvalidKeyException. Do you have unlimited strength jurisdiction files installed?",
          e);
    } catch (GeneralSecurityException e) {
      throw new CryptorException("Failed to decrypt message.", e);
    }
  }
1021145
def cram_md5_response(secret, challenge)
      tmp = Digest::MD5.digest(cram_secret(secret, IMASK) + challenge)
      Digest::MD5.hexdigest(cram_secret(secret, OMASK) + tmp)
    end
989555
public static byte[] createECDHSecret(byte[] seckey, byte[] pubkey) throws AssertFailException {
        Preconditions.checkArgument(seckey.length <= 32 && pubkey.length <= 65);

        ByteBuffer byteBuff = nativeECDSABuffer.get();
        if (byteBuff == null || byteBuff.capacity() < 32 + pubkey.length) {
            byteBuff = ByteBuffer.allocateDirect(32 + pubkey.length);
            byteBuff.order(ByteOrder.nativeOrder());
            nativeECDSABuffer.set(byteBuff);
        }
        byteBuff.rewind();
        byteBuff.put(seckey);
        byteBuff.put(pubkey);

        byte[][] retByteArray;
        r.lock();
        try {
            retByteArray = secp256k1_ecdh(byteBuff, Secp256k1Context.getContext(), pubkey.length);
        } finally {
            r.unlock();
        }

        byte[] resArr = retByteArray[0];
        int retVal = new BigInteger(new byte[] { retByteArray[1][0] }).intValue();

        assertEquals(resArr.length, 32, "Got bad result length.");
        assertEquals(retVal, 1, "Failed return value check.");

        return resArr;
    }
224369
public byte[] decrypt(byte[] cipherData) throws SignatureException {
    checkArgument(cipherData.length >= OVERHEAD_SIZE,
        "Invalid cipherData, %s bytes", cipherData.length);

    // workBytes := initVector || E(payload) || I(signature)
    byte[] workBytes = cipherData.clone();
    ByteBuffer workBuffer = ByteBuffer.wrap(workBytes);
    boolean success = false;

    try {
      // workBytes := initVector || payload || I(signature)
      xorPayloadToHmacPad(workBytes);
      // workBytes := initVector || payload || I'(signature)
      int confirmationSignature = hmacSignature(workBytes);
      int integritySignature = workBuffer.getInt(workBytes.length - SIGNATURE_SIZE);
      workBuffer.putInt(workBytes.length - SIGNATURE_SIZE, confirmationSignature);

      if (confirmationSignature != integritySignature) {
        throw new SignatureException("Signature mismatch: "
            + Integer.toHexString(confirmationSignature)
            + " vs " + Integer.toHexString(integritySignature));
      }

      if (logger.isDebugEnabled()) {
        logger.debug(dump("Decrypted", cipherData, workBytes));
      }

      success = true;
      return workBytes;
    } finally {
      if (!success && logger.isDebugEnabled()) {
        logger.debug(dump("Decrypted (failed)", cipherData, workBytes));
      }
    }
  }
745782
static public byte[] getPreNTLMResponse( String password, byte[] challenge ) {
        byte[] p14 = new byte[14];
        byte[] p21 = new byte[21];
        byte[] p24 = new byte[24];
        byte[] passwordBytes;
        try {
            passwordBytes = password.toUpperCase().getBytes( ServerMessageBlock.OEM_ENCODING );
        } catch( UnsupportedEncodingException uee ) {
            throw new RuntimeException("Try setting jcifs.smb1.encoding=US-ASCII", uee);
        }
        int passwordLength = passwordBytes.length;

        // Only encrypt the first 14 bytes of the password for Pre 0.12 NT LM
        if( passwordLength > 14) {
            passwordLength = 14;
        }
        System.arraycopy( passwordBytes, 0, p14, 0, passwordLength );
        E( p14, S8, p21);
        E( p21, challenge, p24);
        return p24;
    }
881612
public static byte[] hmacSHA1(byte[] data, byte[] key) {
        checkNotNull(data);
        checkNotNull(key);
        HMac hMac = new HMac(new SHA1Digest());
        KeyParameter keyParameter = new KeyParameter(key);
        hMac.init(keyParameter);
        hMac.update(data, 0, data.length);
        byte[] result = new byte[hMac.getMacSize()];
        hMac.doFinal(result, 0);
        return result;
    }
782493
def fost_hmac_request_signature(
    secret, method, path, timestamp, headers = {}, body = ''):
    
    signed_headers, header_values = 'X-FOST-Headers', []
    for header, value in headers.items():
        signed_headers += ' ' + header
        header_values.append(value)
    return fost_hmac_request_signature_with_headers(
        secret, method, path, timestamp,
        [signed_headers] + header_values, body)
640278
public byte[] createSecret (PublicKeyCredentials pkcred, PrivateKey key, int length)
    {
        _data = new AuthResponseData();
        byte[] clientSecret = pkcred.getSecret(key);
        if (clientSecret == null) {
            _data.code = FAILED_TO_SECURE;
            return null;
        }
        byte[] secret = SecureUtil.createRandomKey(length);
        _data.code = StringUtil.hexlate(SecureUtil.xorBytes(secret, clientSecret));
        return secret;
    }
119726
public byte[] generateSignature(@NonNull byte[] content) {
        try {
            Mac mac = Mac.getInstance(HASH_ALGORITHM);
            mac.init(secretKeySpec);
            return mac.doFinal(content);
        } catch (NoSuchAlgorithmException | InvalidKeyException e) {
            // "HmacSHA256" is always supported in Java 8 platform.
            //   (see https://docs.oracle.com/javase/8/docs/api/javax/crypto/Mac.html)
            // All valid-SecretKeySpec-instance are not InvalidKey.
            //   (because the key for HmacSHA256 can be of any length. see RFC2104)
            throw new IllegalStateException(e);
        }
    }
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);
        }
    }
170131
public static HMac hmac(HmacAlgorithm algorithm, String key) {
		return new HMac(algorithm, StrUtil.utf8Bytes(key));
	}
933296
def signature(secret, parts):
    
    if not isinstance(secret, six.binary_type):
        secret = secret.encode('utf-8')
    newparts = []
    for part in parts:
        if not isinstance(part, six.binary_type):
            part = part.encode('utf-8')
        newparts.append(part)
    parts = newparts
    if sys.version_info >= (2, 5):
        csum = hmac.new(secret, digestmod=hashlib.sha1)
    else:
        csum = hmac.new(secret, digestmod=sha)

    for part in parts:
        csum.update(part)

    return csum.hexdigest()
1018055
public byte[] computeSharedSecret(Key key) throws InvalidKeyException {
		keyAgreement.doPhase(key, true);
		return keyAgreement.generateSecret();
	}
331555
public static String hmacSha256(String input, String secretKey) {
        if (input == null) {
            throw new NullPointerException("input");
        } else if (secretKey == null) {
            throw new NullPointerException("secretKey");
        }
        return hmacSha256(input.getBytes(CHARSET_UTF8), secretKey.getBytes(CHARSET_UTF8));
    }