简体   繁体   English

无法使用 SHA-256 创建 checkSum 值

[英]Unable to create checkSum value using SHA-256

As per my requirement I want to create checksum value using SHA-256, from InputStream,根据我的要求,我想使用来自 InputStream 的 SHA-256 创建校验和值,

As below:如下:

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.您不需要这样做:将输入存储在(非压缩)字节数组中就足够了。

I think you need to make sure that in.read() works as intended (and that there's actually some data to read).我认为您需要确保 in.read() 按预期工作(并且实际上有一些数据要读取)。 You get the checksum for a null input and your zip entry is also empty, so it looks like the input was empty.您获得空输入的校验和,并且您的 zip 条目也是空的,因此看起来输入是空的。 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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM