Nginx-sticky-module C tansfer Java

针对nginx-sticky 模块的md5算法,官方网站给出了两种返回结果的方法

第一种:

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class NgxHttpStickyMisc {
    private static final int MD5_CBLOCK = 64;
    private static final int MD5_LBLOCK = (MD5_CBLOCK / 4);
    private static final int MD5_DIGEST_LENGTH = 16;

    // Rest of your code

    public static int ngxHttpStickyMiscMd5(byte[] in, int len, StringBuilder digest) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(in, 0, len);
            byte[] hash = md5.digest();

            StringBuilder hexDigest = new StringBuilder();
            for (byte b : hash) {
                hexDigest.append(String.format("%02x", b));
            }

            digest.setLength(0);
            digest.append(hexDigest.toString());

            return 0; // NGX_OK
        } catch (NoSuchAlgorithmException e) {
            // Handle the exception or return an error code (e.g., NGX_ERROR)
            return -1; // NGX_ERROR
        }
    }

    public static void main(String[] args) {
        String input = "your_input_data"; // Replace with your input data
        byte[] inputBytes = input.getBytes();
        StringBuilder digest = new StringBuilder(32); // MD5_DIGEST_LENGTH * 2

        int result = ngxHttpStickyMiscMd5(inputBytes, inputBytes.length, digest);

        if (result == 0) {
            System.out.println("MD5 Digest: " + digest.toString());
        } else {
            System.out.println("Error calculating MD5 digest.");
        }
    }

}

第二种:

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

public class NgxHttpStickyMisc {

    public static int ngxHttpStickyMiscHmacMd5(byte[] in, int len, byte[] key, StringBuilder digest) {
        try {
            if (key.length > MD5_CBLOCK) {
                MessageDigest md5 = MessageDigest.getInstance("MD5");
                md5.update(key, 0, key.length);
                key = md5.digest();
            }

            byte[] k = Arrays.copyOf(key, MD5_CBLOCK);

            for (int i = 0; i < MD5_CBLOCK; i++) {
                k[i] ^= 0x36;
            }

            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(k, 0, k.length);
            md5.update(in, 0, len);
            byte[] hash = md5.digest();

            for (int i = 0; i < MD5_CBLOCK; i++) {
                k[i] ^= 0x6a;
            }

            md5.reset();
            md5.update(k, 0, k.length);
            md5.update(hash, 0, MD5_DIGEST_LENGTH);
            hash = md5.digest();

            StringBuilder hexDigest = new StringBuilder();
            for (byte b : hash) {
                hexDigest.append(String.format("%02x", b));
            }

            digest.setLength(0);
            digest.append(hexDigest.toString());

            return 0; // NGX_OK
        } catch (NoSuchAlgorithmException e) {
            // Handle the exception or return an error code (e.g., NGX_ERROR)
            return -1; // NGX_ERROR
        }
    }

    private static final int MD5_DIGEST_LENGTH = 16;
    private static final int MD5_CBLOCK = 64;

    public static void main(String[] args) {
        String input = "your_input_data"; // Replace with your input data
        byte[] inputBytes = input.getBytes();
        String keyStr = "your_key"; // Replace with your key
        byte[] keyBytes = keyStr.getBytes();
        StringBuilder digest = new StringBuilder(32); // MD5_DIGEST_LENGTH * 2

        int result = ngxHttpStickyMiscHmacMd5(inputBytes, inputBytes.length, keyBytes, digest);

        if (result == 0) {
            System.out.println("HMAC-MD5 Digest: " + digest.toString());
        } else {
            System.out.println("Error calculating HMAC-MD5 digest.");
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值