Client hash encryption (JavaScript hash encryption, source code)

Source: Internet
Author: User
Tags base64 hmac md5 hash sha1 account security

Summary

It is difficult to imagine users in what kind of network environment using our development of the application, if the user is in a network environment is not a trusted environment, then the user's account security may be a threat, such as the user logged on the account password submitted by the network sniffer theft, client-side encryption data can effectively defend the network sniffer to steal data , it improves the safety factor of the system by encrypting the data after client JavaScript and submitting it to the server, which conforms to the defense-in-depth principle and the confidentiality of data security elements; This paper deals with two commonly used encryption algorithms: MD5 and SHA1;

Hashencrypt Encrypted objects

The cryptographic algorithm encapsulates the Hashencrypt object, contains the MD5, SHA1 two methods, and Hashformat attributes, and also expands the string type of the system, adding MD5 and SHA1 methods to its instance;

Property

Hashformat: Output Format enumeration, object type, static structure: {Base64: "Base64", Hex: "Hex", String: "string"}

Method

MD5 method

HASHENCRYPT.MD5 (String,ascii,hexuppercase)

String type: String, required for encrypted content.

ASCII type: bool, optional, encrypted with ASCII character encoding, Unicode by default

Hexuppercase type: bool, optional, hexadecimal encoding of the output is uppercase

HASHENCRYPT.MD5 (STRING,BASE64PAD,ASCII)

String type: String, the content to be encrypted

ASCII type: BOOL, whether encrypted in ASCII character encoding, Unicode by default

Base64pad Type: string, the padding character of the output base64 is "=" by default, and if the parameter is set to a string then the output format is Base64 encoded by default.

HASHENCRYPT.MD5 (String,hexuppercase)

String type: String, the content to be encrypted

Hexuppercase Type: number,1 indicates that the hexadecimal encoding of the output is uppercase;

HASHENCRYPT.MD5 (string,options)

String type: String, the content to be encrypted

Options type: Object, encrypted parameter option;

Options Property Description:

ASCII type: BOOL, whether encrypted in ASCII character encoding, Unicode by default

Base64 Type: String, the padding character of the output base64 is "=" by default, and if the parameter is set to a string then the output format is Base64 encoded by default.

Hexuppercase Type: bool, whether the hexadecimal encoding of the output is uppercase

Format type: Hashencrypt.hashformat enumeration type, indicating the output format of ciphertext, optional values are:

"Base64" Base64 encoding format, "hex" hexadecimal encoding format, "string" directly convert string format;

Hmackey Type: string, optional, computes a hash-based message authentication code (HMAC) using the MD5 hash function,

This HMAC process mixes the key with the message data, uses a hash function to hash the mixed result, mixes the resulting hash value with the key, and then applies the hash function again. The hash value of the output is 128 bits long.

Example:

<script>        var data = hashencrypt.md5 ("Hello world!",false,false);        //"Hello world!". MD5 (FALSE,FALSE)//This line of code has the same effect as the result document.write (data);</script>      

Output Result: 99da75326ffaf6acc0debe844b359894

SHA1 method

The signature of this method is exactly the same as the MD5 method, and here it is not restated, hahaha.

Code Download:

Source Code and Method demo

Http://files.cnblogs.com/Jackson-Bruce/%E5%AE%A2%E6%88%B7%E7%AB%AF%E5%93%88%E5%B8%8C%E5%8A%A0%E5%AF%86.zip

Release version

Http://files.cnblogs.com/Jackson-Bruce/HashEncrypt.min.js

Related information:

Http://pajhome.org.uk/crypt/md5

Client hash encryption (JavaScript hash encryption, source code)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.