无法使用SHA-256创建checkSum值 [英] Unable to create checkSum value using SHA-256

查看:103
本文介绍了无法使用SHA-256创建checkSum值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的要求,我想使用SHA-256从InputStream创建校验和值。

As per my requirement I want to create checksum value using SHA-256, from InputStream,

如下所示:

private InputStream createZipInput(List<ResponsePack> aList, byte[] manifestData)
    {
        final int bufferSize = 2048;
        byte buffer[] = new byte[bufferSize];
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        ZipOutputStream zipFileToSend = new ZipOutputStream(byteStream);
        LOG.trace("Compressing the file {}");
        try
        {
            for (ResponsePack info : aList)
            {
                ByteArrayOutputStream byteStreamCheckSum = new ByteArrayOutputStream();
                ZipOutputStream zipFileToSendCheckSum = new ZipOutputStream(byteStreamCheckSum);
                zipFileToSend.putNextEntry(new ZipEntry(info.getFileName()));
                zipFileToSendCheckSum.putNextEntry(new ZipEntry(info.getFileName()));
                InputStream in = info.getFileContentStream();
                int length;
                while ((length = in.read(buffer)) >= 0)
                {
                    zipFileToSend.write(buffer, 0, length);
                    zipFileToSendCheckSum.write(buffer, 0, length);
                }
                zipFileToSend.closeEntry();
                zipFileToSendCheckSum.closeEntry();
                String checksum = validChecksum(byteStreamCheckSum.toByteArray());
                LOG.error("Checksum {}", checksum);
                zipFileToSendCheckSum.flush();
                zipFileToSendCheckSum.close();
            }
            zipFileToSend.close();
        }
        catch (IOException e)
        {
            return e;
        }
        return new ByteArrayInputStream(byteStream.toByteArray());
    }
    
    private static String validChecksum(byte[] dataCopy)
    {
        printLOG("Byte Array Size {}", dataCopy.length);
        try (ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(dataCopy)))
        {
            ZipEntry zipEntry;
            MessageDigest digest = DigestUtils.getSha256Digest();
            DWriter writer = new DWriter(digest);
            while ((zipEntry = zipInputStream.getNextEntry()) != null)
            {
                org.apache.commons.io.output.ByteArrayOutputStream dest = StreamUtils.extractFileAsByteArrayStream(zipInputStream);
                LOG.error("CheckSum Entity creating");
                if(zipEntry != null)
                {
                    printLOG("CheckSum Entity file Name {}", zipEntry.getName());
                }
                LOG.error("Byte array size {}", dest.toByteArray().length);
                writer.write(dest.toByteArray());
                dest.flush();
                dest.close();
            }
            
            if (writer.getChecksum() != null)
            {
                return writer.getChecksum();
            }
            else
            {
                return "";
            }
        }
        catch (Exception e)
        {
            printLOG("Exception encountered while creating checksum: {}", e.getMessage());
            return "";
        }
    }
    
    static class DWriter
    {
        private final MessageDigest myDigest;

        DWriter(MessageDigest digest)
        {
            myDigest = digest;
        }

        public void write(byte[] data)
        {
            myDigest.update(data);
        }

        public String getChecksum()
        {
            return new String(Hex.encodeHex(myDigest.digest()));
        }
    }

但是问题是当我检查日志时,发现字节数组包含值但仍然始终为空字符串创建校验和,如下所示

But the problem is when I checked the log, found byte array contains value but still checksum always creating for empty string, as below

Byte Array Size 3948
CheckSum Entity creating
CheckSum Entity file Name 20200911104812526.json
Byte array size 20854
Checksum e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

在我做错的地方帮我,由于这个原因,我得到一个空字符串的校验和

Help me where I am doing wrong, due to which I am getting checksum for an empty string

推荐答案

我不确定代码到底出了什么问题,但似乎太过分了复杂:您要将输入写入压缩的流中,然后将其解压缩到内存中以再次读取。

I'm not sure what's wrong with the code but it seems overly complicated: you're writing the input into a zipped stream and the dezip it in memory to read it again.

您不需要这样做:将输入存储在(非压缩后的字节数组就足够了。

You don't need to do that: storing the input in a (non-zipped) byte array should be enough.

我认为您需要确保in.read()可以按预期工作(并且实际上有一些数据可以读取)。
您得到一个空输入的校验和,并且您的zip条目也为空,因此看起来输入为空。添加一些日志或使用调试器来调查正在发生的事情。

I think you need to make sure that in.read() works as intended (and that there's actually some data to read). You get the checksum for a null input and your zip entry is also empty, so it looks like the input was empty. Add some logs or use a debugger to investigate what's happening.

private InputStream createZipInput(List<ResponsePack> aList, byte[] manifestData) {
    final int bufferSize = 2048;
    byte buffer[] = new byte[bufferSize];
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    ZipOutputStream zipFileToSend = new ZipOutputStream(byteStream);
    LOG.trace("Compressing the file {}");
    try {
        for (ResponsePack info : aList) {
            ByteArrayOutputStream byteStreamCheckSum = new ByteArrayOutputStream();
            zipFileToSend.putNextEntry(new ZipEntry(info.getFileName()));
            InputStream in = info.getFileContentStream();
            int length;
            while ((length = in.read(buffer)) != -1) {
                zipFileToSend.write(buffer, 0, length);
                byteStreamCheckSum.write(buffer, 0, length);
            }
            zipFileToSend.closeEntry();
            
            MessageDigest digest = DigestUtils.getSha256Digest();
            digest.update(byteStreamCheckSum.toByteArray());
            String checksum = new String(Hex.encodeHex(digest.digest()));
            
            LOG.error("Checksum {}", checksum);
            
        }
        zipFileToSend.close();
    } catch (IOException e) {
        throw e;
    }
    return new ByteArrayInputStream(byteStream.toByteArray());

这篇关于无法使用SHA-256创建checkSum值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆