CryptoJS可以进行二进制散列吗? [英] Is binary hashing possible with CryptoJS?

查看:544
本文介绍了CryptoJS可以进行二进制散列吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用类似于 SpeakEasy <的

I want to create an HOTP client using javascript similar to SpeakEasy

以上库用于服务器端javascript使用,它使用NodeJS。

The above library is intended for server side javascript usage and it uses NodeJS.

我想要在浏览器中的前端javascript上执行相同的操作,但我无法使用CryptoJS来实现此行为。

I want to do the same thing on front end javascript in a browser but I haven't been able to use CryptoJS to achieve this behavior.

         var key = "abc";
         var counter = "123";

         // create an octet array from the counter
         var octet_array = new Array(8);

         var counter_temp = counter;

         for (var i = 0; i < 8; i++) {
             var i_from_right = 7 - i;

             // mask 255 over number to get last 8
             octet_array[i_from_right] = counter_temp & 255;

             // shift 8 and get ready to loop over the next batch of 8
             counter_temp = counter_temp >> 8;
         }

        // There is no such class called as Buffer on Browsers (its node js)
         var counter_buffer = new Buffer(octet_array);

         var hash = CryptoJS.HmacSHA1(key,counter_buffer);

         document.write("hex value "+ hash);
         document.write("hash value "+    CryptoJS.enc.Hex.stringify(hash));

我知道这可以在java(android)或objective c(ios)$等原生平台上实​​现b $ b以下是相应的实施目标C中的HOTP 但我怀疑是否可以在基于网络的前端上做。

I know this is possible on a native platform like java (android) or objective c (ios) Here is the corresponding implementation HOTP in Objective C but I doubt if it's possible to do on a web based front end.

另外,我非常怀疑这样的东西在浏览器中是否安全,因为可以从任何浏览器查看javascript。任何输入建议都会有用。我这样做是为了POC。我很好奇是否有人在基于Web的平台上使用Hotp。

Also, I highly doubt if such a thing is secure in browser because javascript is viewable from any browser. Any inputs suggestions would be useful. I am doing this for a POC. I am curious if anyone has used Hotp on web based platform.

推荐答案

没有这样的语言支持二进制数据字符串码。您需要将二进制数据编码为某种格式(如Hex或Base64),然后让CryptoJS将其解码为自己的内部二进制格式,然后您可以将其传递给各种CryptoJS函数:

There is no such language that supports binary data strings in the code. You need to encode the binary data into some format such as Hex or Base64 and let CryptoJS decode it into it's own internal binary format which you then can pass to the various CryptoJS functions:

var wordArrayFromUtf = CryptoJS.enc.Utf8.parse("test");
var wordArrayFromHex = CryptoJS.enc.Hex.parse("74657374"); // "test"
var wordArrayFromB64 = CryptoJS.enc.Base64.parse("dGVzdA=="); // "test"

其他功能是:

wordArrayFromHex.toString(CryptoJS.enc.Utf8)  // "test"
CryptoJS.enc.Utf8.stringify(wordArrayFromB64) // "test"

如果将字符串传递给CrypoJS函数(这里不是这些函数),则假定它是Utf8编码的串。如果你不想那样,你需要自己解码。

If you pass a string into a CrypoJS function (not these here), it will be assumed to be a Utf8-encoded string. If you don't want that, you need to decode it yourself.

这篇关于CryptoJS可以进行二进制散列吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆