1. 程式人生 > >JavaScript 加密庫Crypto-JS

JavaScript 加密庫Crypto-JS

CryptoJS

CryptoJS是一個純javascript寫的加密類庫,使用起來簡單方便。目前已支援的演算法包括:

  • MD5
  • SHA-1
  • SHA-256
  • AES
  • Rabbit
  • MARC4
  • HMAC
    • HMAC-MD5
    • HMAC-SHA1
    • HMAC-SHA256
  • PBKDF2

具體介紹和下載連結地址:https://code.google.com/archive/p/crypto-js/

CryptoJS庫的使用

CryptoJS的使用很方便,只需引用相關的庫檔案即可。

MD5


MD5 is a widely used hash function. It's been used in a variety of security applications and is also commonly used to check the integrity of files. Though, MD5 is not collision resistant, and it isn't suitable for applications like SSL certificates or digital signatures that rely on this property.

```
var hash = CryptoJS.MD5("Message");

```

SHA-1


The SHA hash functions were designed by the National Security Agency (NSA). SHA-1 is the most established of the existing SHA hash functions, and it's used in a variety of security applications and protocols. Though, SHA-1's collision resistance has been weakening as new attacks are discovered or improved.

```
var hash = CryptoJS.SHA1("Message");

```

SHA-2


SHA-256 is one of the four variants in the SHA-2 set. It isn't as widely used as SHA-1, though it appears to provide much better security.

```
var hash = CryptoJS.SHA256("Message");

```

SHA-512 is largely identical to SHA-256 but operates on 64-bit words rather than 32.

```
var hash = CryptoJS.SHA512("Message");

```