当前位置: 首页 > 面试题库 >

MD5签署HttpServletResponse

谭修然
2023-03-14
问题内容

我正在寻找一种检查a的内容HttpServletResponse以使用MD5哈希对其进行签名的方法。

伪代码可能看起来像这样

process(Response response, Request request){

defaultProcessingFor(response,request);

dispatcher.handle(response,request);

// Here I want to read the contents of the Response object (now filled with data) to create a MD5 hash with them and add it to a header.
}

那可能吗?


问题答案:

是的,那是可能的。您需要借助以下方法来修饰响应:在HttpServletResponseWrapper其中,您可以用ServletOutputStream自定义实现替换,该实现将字节写入MD5摘要和“原始”输出流。最后提供一个访问器以获得最终的MD5和。

更新 我只是为了好玩而已,下面是一个启动示例:

响应包装器:

public class MD5ServletResponse extends HttpServletResponseWrapper {

    private final MD5ServletOutputStream output;
    private final PrintWriter writer;

    public MD5ServletResponse(HttpServletResponse response) throws IOException {
        super(response);
        output = new MD5ServletOutputStream(response.getOutputStream());
        writer = new PrintWriter(output, true);
    }

    public PrintWriter getWriter() throws IOException {
        return writer;
    }

    public ServletOutputStream getOutputStream() throws IOException {
        return output;
    }

    public byte[] getHash() {
        return output.getHash();
    }

}

MD5输出流:

public class MD5ServletOutputStream extends ServletOutputStream {

    private final ServletOutputStream output;
    private final MessageDigest md5;

    {
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            throw new ExceptionInInitializerError(e);
        }
    }

    public MD5ServletOutputStream(ServletOutputStream output) {
        this.output = output;
    }

    public void write(int i) throws IOException {
        byte[] b = { (byte) i };
        md5.update(b);
        output.write(b, 0, 1);
    }

    public byte[] getHash() {
        return md5.digest();
    }

}

如何使用它:

// Wrap original response with it:
MD5ServletResponse md5response = new MD5ServletResponse(response);

// Now just use md5response instead or response, e.g.:
dispatcher.handle(request, md5response);

// Then get the hash, e.g.:
byte[] hash = md5response.getHash();
StringBuilder hashAsHexString = new StringBuilder(hash.length * 2);
for (byte b : hash) {
    hashAsHexString.append(String.format("%02x", b));
}
System.out.println(hashAsHexString); // Example af28cb895a479397f12083d1419d34e7.


 类似资料:
  • MD5签名SDK下载(JAVA/PHP/.NET):https://www.wenjiangs.com/doc/RV0OpbQJLtMD5SignSDK.zip Java MD5签名实现 参考示例:http://blog.csdn.net/u011627980/article/details/52778326 签名规则: 变现猫与开发者之间所有的请求进行md5签名,确保传输的安全可靠。 签名原理:

  • 在定点数学中,我使用了大量16位信号,并用32位中间结果进行乘法运算。例如: 假设a是一个q14数,那么c与b的比例相同。 这很好,适用于有符号和无符号算术。 问题是:如果我混合类型会发生什么?例如,如果我知道乘数“a”总是在 0.0 到 1.0 之间,那么很容易使其成为无符号的 int q15 以获得额外的精度(并将移位计数更改为 15)。但是,我从来不明白如果您尝试在 C 中将有符号和无符号数

  • Git 虽然是密码级安全的,但它不是万无一失的。 如果你从因特网上的其他人那里拿取工作,并且想要验证提交是不是真正地来自于可信来源,Git 提供了几种通过 GPG 来签署和验证工作的方式。 GPG 介绍 首先,在开始签名之前你需要先配置 GPG 并安装个人密钥。 $ gpg --list-keys /Users/schacon/.gnupg/pubring.gpg ----------------

  • md5

    该工具直接依赖于 blueimp-md5 注意: md5是消息摘要算法并非加密算法,用于需要加密的场景会有安全问题。 import { md5 } from 'vux' md5('VUX')

  • md5

    import "crypto/md5" md5包实现了MD5哈希算法,参见RFC 1321。 Constants func Sum(data []byte) [Size]byte func New() hash.Hash Examples New Sum const BlockSize = 64 MD5字节块大小。 const Size = 16 MD5校验和字节数。 func Sum func

  • 我的代码 有办法在邮件上签名吗?当我使用上述代码测试发送电子邮件时,我收到了电子邮件,但我收到了错误 根据gmail,他们将签名数据附加到标题以进行身份验证 我用gmail imap发送邮件 有没有办法使用php imap对电子邮件进行身份验证?