首页 文章

填充长胡子的问题

提问于
浏览
0

对于加密专家,我最近有一个问题出现在我脑海中 . 因此,例如,认为我们有一个很长的字节串,我们希望将该字符串放入一个哈希函数中,为了便于说明,我们可以将其作为SHA1 . 我们知道,SHA1以64字节块的形式接收输入,每个散列函数afaik需要在处理之前填充消息 . 现在问题是它是需要填充的最后一个块还是整个字符串?这很重要,因为在填充结束时我们会追加长度 . 谢谢大家 .

1 回答

  • 0

    现在的问题是,它是需要填充的最后一个块还是整个字符串?

    我相信两件事都是一样的 . 填充整个字符串意味着仅填充最后一个块 .

    来自好老wiki的一些伪代码here

    查看代码可能会给您一些见解:

    摘自:mattmahoney.net/dc/sha1.c

    void SHA1PadMessage(SHA1Context *context)
    {
        /*
         *  Check to see if the current message block is too small to hold
         *  the initial padding bits and length.  If so, we will pad the
         *  block, process it, and then continue padding into a second
         *  block.
         */
        if (context->Message_Block_Index > 55)
        {
            context->Message_Block[context->Message_Block_Index++] = 0x80;
            while(context->Message_Block_Index < 64)
            {
                context->Message_Block[context->Message_Block_Index++] = 0;
            }
    
            SHA1ProcessMessageBlock(context);
    
            while(context->Message_Block_Index < 56)
            {
                context->Message_Block[context->Message_Block_Index++] = 0;
            }
        }
        else
        {
            context->Message_Block[context->Message_Block_Index++] = 0x80;
            while(context->Message_Block_Index < 56)
            {
    
                context->Message_Block[context->Message_Block_Index++] = 0;
            }
        }
    
        /*
         *  Store the message length as the last 8 octets
         */
        context->Message_Block[56] = context->Length_High >> 24;
        context->Message_Block[57] = context->Length_High >> 16;
        context->Message_Block[58] = context->Length_High >> 8;
        context->Message_Block[59] = context->Length_High;
        context->Message_Block[60] = context->Length_Low >> 24;
        context->Message_Block[61] = context->Length_Low >> 16;
        context->Message_Block[62] = context->Length_Low >> 8;
        context->Message_Block[63] = context->Length_Low;
    
        SHA1ProcessMessageBlock(context);
    }
    

相关问题