<div dir="ltr">hi all:<div>      I built a module for calculate the SHA-1. The code is:</div><div>##################</div><div><div>#include &lt;linux/init.h&gt;</div><div>#include &lt;linux/module.h&gt;</div><div>#include &lt;linux/kernel.h&gt;</div><div>#include &lt;linux/crypto.h&gt;</div><div>#include &lt;linux/err.h&gt;</div><div>#include &lt;linux/scatterlist.h&gt;</div><div>MODULE_LICENSE(&quot;Dual BSD/GPL&quot;);</div><div><br></div><div>#define SHA1_LENGTH     20</div><div><br></div><div>static int hello_init(void)</div><div>{</div><div>    /*  </div><div>     * <a href="http://lxr.oss.org.cn/source/fs/ecryptfs/crypto.c?v=2.6.30">http://lxr.oss.org.cn/source/fs/ecryptfs/crypto.c?v=2.6.30</a></div><div>     */</div><div>    struct scatterlist sg; </div><div>    struct hash_desc desc;</div><div><br></div><div>    //way 1     </div><div>    /*  </div><div>    char *plaintext = &quot;c&quot;;</div><div>    size_t len = strlen(plaintext);</div><div>    */</div><div><br></div><div>    //way 2</div><div>    /*  </div><div>    char *plaintext = kmalloc(sizeof(char), GFP_KERNEL);</div><div>    plaintext = &quot;c&quot;;    </div><div>    size_t len = 1;</div><div>    */</div><div><br></div><div>    // way 3.</div><div>    /*  </div><div>    char plaintext[1] = {&#39;c&#39;};</div><div>    size_t len = 1;</div><div>    */</div><div><br></div><div>    // way 4.</div><div>    /*  </div><div>    char *plaintext = (char *)__get_free_page(GFP_KERNEL);</div><div>    memcpy(plaintext, &quot;c&quot;, 1);</div><div>    size_t len = 1;</div><div>    */</div></div><div><br></div><div><div>    int rc = 0;</div><div>    int i;  </div><div>    char hashtext[SHA1_LENGTH];</div><div><br></div><div>    memset(hashtext, 0x00, SHA1_LENGTH);</div><div>    printk(KERN_INFO &quot;sha1: %s\n&quot;, __FUNCTION__);</div><div><br></div><div>    sg_init_one(&amp;sg, plaintext, len);</div><div>    desc.tfm = crypto_alloc_hash(&quot;sha1&quot;, 0, CRYPTO_ALG_ASYNC);</div><div>    desc.flags = 0;</div><div><br></div><div>    rc = crypto_hash_init(&amp;desc);</div><div>    if (rc) {</div><div>        printk(KERN_ERR &quot;%s: Error initializing crypto hash; rc = [%d]\n&quot;, __func__, rc);</div><div>        goto out;</div><div>    }</div><div>    rc = crypto_hash_update(&amp;desc, &amp;sg, len);</div><div>    if (rc) {</div><div>        printk(KERN_ERR &quot;%s: Error updating crypto hash; rc = [%d]\n&quot;, __func__, rc);</div><div>        goto out;</div><div>    }</div><div>    rc = crypto_hash_final(&amp;desc, hashtext);</div><div>    if (rc) {</div><div>        printk(KERN_ERR &quot;%s: Error finalizing crypto hash; rc = [%d]\n&quot;, __func__, rc);</div><div>        goto out;</div><div>    }</div><div>    crypto_free_hash(desc.tfm);</div><div><br></div><div>    for (i = 0; i &lt; 20; i++) {</div><div>        printk(KERN_INFO &quot;%02x-%d\n&quot;, hashtext[i]&amp;0xff, i);</div><div>    }</div><div><br></div><div>out:</div><div>    printk(KERN_INFO &quot;end\n&quot;);</div><div>    return rc;</div><div>}</div></div><div><br></div><div><div>static void hello_exit(void)</div><div>{</div><div>    printk(KERN_ALERT &quot;Goodbye, cruel world\n&quot;);</div><div>}</div><div><br></div><div>module_init(hello_init);</div><div>module_exit(hello_exit);</div></div><div>##################</div><div>I can just get the right result by way 3 and 4, In way 1 and 2 , I get the wrong result, </div><div>I check the result by link: <a href="http://www.xorbin.com/tools/sha1-hash-calculator">http://www.xorbin.com/tools/sha1-hash-calculator</a> .</div><div>Please tell me why? Thank you.</div><div><br></div><div><br></div><div><div><br></div></div></div>