2017-10-18 48 views
6

我想在我的客户端使用手指打印,并获得作为更大的代码的一部分这一代码的持有。隐藏我的哈希数字在javascript

function checksum(str) { 
    var hash = 5382, 
     i = str.length; 

    while (i--) hash = (hash * 33)^str.charCodeAt(i); 

    return hash >>> 0; 
} 

正如你所看到的散列很明显。你能告诉我如何使用或使用什么实现,以便我可以隐藏或掩盖散列= 5382的任何内容。谢谢。

+3

只能混淆它,而不是真正的“隐藏” – haim770

+0

您可以使用'CryptoJS':https://stackoverflow.com/questions/18279141/javascript-string-encryption-and-decryption –

+0

你需要像[RSA](http://www.ohdave.com/rsa/)这样的非对称算法。 – ceving

回答

0

如果您使用base64编码,但任何人都可以轻松解码。你的散列有多敏感?

str = "The quick brown fox jumps over the lazy dog"; 
b64 = btoa(unescape(encodeURIComponent(str))); 
str = decodeURIComponent(escape(window.atob(b64))); 

输出将是VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw ==

如果你正在使用PHP你只BASE64_ENCODE()和BASE64_DECODE()来处理。例如,你可以使用编码值隐藏输入,然后获取它的val值并使用我给你的最后一行。

Base64 PHP http://php.net/manual/en/function.base64-encode.php and base64 JAVASCRIPT https://developer.mozilla.org/pt-BR/docs/Web/API/WindowBase64/atob。或者你可以加密它的内容然后解密服务器端。下面有一个小类加密/解密数据(PHP):

<?php 
namespace Company\Security; 

/* 
* @description: Simple class to wrap crypt function calls 
* @author: Marco A. Simao 
*/ 

class Crypto { 

/* 
* returns encrypted data with iv appended at the begining of the string 
*/ 
public static function encrypt($data, $key) 
{ 
    $iv = openssl_random_pseudo_bytes(16); 

    $c = openssl_encrypt($data, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv); 

    return $iv . $c; 
} 

/* 
* returns decrypted data. Expects 16 first bytes of data to be iv table. 
*/ 
public static function decrypt($data, $key) 
{ 
    return openssl_decrypt(substr($data, 16), 'AES-128-CBC', $key, OPENSSL_RAW_DATA, substr($data, 0, 16)); 
} 
} 

而且你需要在JavaScript中解密,如:How to use the Web Crypto API to decrypt a file created with OpenSSL?

+0

这看起来不错。你能告诉我一个它的样例实现吗? –

+1

@stackquestions - 请记住这不安全,任何客户端都可以自己重写脚本并查看结果。如果你的目标是隐藏从偶然的眼睛粗略检查的字符串,这很好,但它不会提供任何“真正的”安全。 –

+0

是的,谢谢你指出,我忘了告诉他,但我问它是多么的敏感:) – Marco