简体   繁体   English

为base64输入数据(base64中的文件数据)生成SHA1哈希ID

[英]generate SHA1 hash ID for base64 input data (file data in base64)

I'm trying to generate SHA1 hash ID for the input, input is nothing but file content in base64 format. 我正在尝试为输入生成SHA1哈希ID,输入不过是base64格式的文件内容。

To be clear input will be base64 data, output will be SHA1 hash ID 为了清楚base64 ,输入将为base64数据,输出将为SHA1哈希ID

I have been using CryptoJS library as shown in below code. 我一直在使用CryptoJS库,如下面的代码所示。 But no luck the generated hash ID is different than the actual hash needed. 但是所产生的哈希ID与实际所需的哈希没有任何不同。

<!doctype html>
<html>
    <head>
        <meta charset="utf-8"/>
        <title></title>
        <link rel="stylesheet" media="all" href=""/>
        <meta name="viewport" content="width=device-width, initial-scale=1"/>
    </head>
    <body lang="en">
        <form id="test">
            <p>
                <label>Text</label><br>
                <textarea id="text" style="width: 500px; height: 200px">This is the secret message</textarea>
            </p>
            <input type="submit" id="submit">
        </form>
        <div id="output"></div>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
        <script src="crypto-js.min.js"></script>
        <script src="sha1.js"></script>
        <script>
            $(function() {
                $('#test').on('submit', function() {
                    var b64data= $('#text').val();
                    var hash = CryptoJS.SHA1(b64data);
                    var result = CryptoJS.enc.Hex.stringify(hash);
                    console.log(result);

                    return false;
                });

            });
        </script>
    </body>
</html>

Output from my code: 我的代码输出:

6bc205c87e54d2d114a880f25d227b75639a9d74 6bc205c87e54d2d114a880f25d227b75639a9d74

Expected output: 预期产量:

fca9cdfd98590aa4c417b412c8331dfb2faf2253 fca9cdfd98590aa4c417b412c8331dfb2faf2253

Input base64 sample: Unable to attach as length exceed Stack Overflow body limit. 输入base64样本:由于长度超过堆栈溢出主体限制而无法连接。

i have fixed my code converting base64 to words array like below 我已经修复了将base64转换为word数组的代码,如下所示

var words = CryptoJS.enc.Base64.parse(b64data);
var hash = CryptoJS.SHA1(words);
var result = CryptoJS.enc.Hex.stringify(hash);

it worked for me. 它为我工作。 hope it will help others as well 希望它也会对其他人有帮助

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

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