简体   繁体   中英

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.

To be clear input will be base64 data, output will be SHA1 hash ID

I have been using CryptoJS library as shown in below code. But no luck the generated hash ID is different than the actual hash needed.

<!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

Expected output:

fca9cdfd98590aa4c417b412c8331dfb2faf2253

Input base64 sample: Unable to attach as length exceed Stack Overflow body limit.

i have fixed my code converting base64 to words array like below

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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