[Open source third-party library] How to use crypto-js encryption algorithm library

The OpenAtom OpenHarmony (referred to as "OpenHarmony") third-party library is a software component that has been verified to be reusable on the OpenHarmony system and can help developers quickly develop OpenHarmony applications. If it is released to the open source community, it is called an open source third-party library, and developers can obtain it by visiting the open source community. Next, let’s learn about the crypto-js open source third-party library. Crypto-js is an encryption algorithm class library that can easily perform its supported encryption and decryption operations on the front end. Currently crypto-js supports the following algorithms: MD5, SHA-1, SHA-256, HMAC, HMAC-MD5, HMAC-SHA1, HMAC-SHA256, PBKDF2, AES, RC4, DES, etc.

Features

• Security and Reliability: crypto-js implements extensively tested and vetted encryption algorithms and is designed to provide safe and reliable encryption. It uses the best practices in cryptography and works to protect the security and integrity of your data.

• Easy-to-use API: crypto-js provides an easy-to-use API that enables developers to easily perform encryption and decryption operations in their applications. It provides intuitive and consistent methods and options that make cryptographic operations more convenient and flexible.

• Multiple encryption algorithm support: crypto-js supports multiple symmetric encryption algorithms (such as AES, DES) and hash functions (such as MD5, SHA1, SHA256), as well as message authentication code (MAC) algorithms. It provides a complete set of encryption tools to meet different security needs.

Usage scenarios of commonly used encryption algorithms

MD5

MD5 is a widely used hash function. It is used in various security applications and is often used to verify the integrity of files. But MD5 is not resistant to collision attacks, so it is not suitable for SSL certificates or digital signatures.

SHA-1

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

SHA-2

SHA-224, SHA-256, SHA-384, and SHA-512 are collectively called SHA-2. SHA-256 is one of four variants in the SHA-2 set. Although it provides better security, it is not as widely used as SHA-1. SHA-512 is largely the same as SHA-256, but is faster than SHA-256 on 64-bit computers (because they use 64-bit arithmetic internally); on 8-bit, 16-bit, and 32-bit computers , SHA-256 is faster than SHA-512.

HMAC

HMAC is a mechanism for message authentication using cryptographic hash functions. It can be used in conjunction with any iterative cryptographic hash function and is commonly used for message authentication, digital visas.

PBKDF2

PBKDF2 is a function used to encrypt user passwords. In many applications of cryptography, user security ultimately depends on user passwords, which require some processing since they often cannot be used directly as keys.

AES

AES Advanced Encryption Standard (AES) in cryptography, also known as Rijndael encryption, is a block encryption standard adopted by the U.S. federal government. This standard is used to replace the original DES (Data Encryption Standard). It has been analyzed by many parties and is widely used around the world.

RC4

The RC4 algorithm is a stream cipher designed by Ron Rivest for RSA in 1987. As a trade secret of RSA, it was not anonymously published on the Internet until 1994. RC4 is used in the SSL/TLS (Secure Sockets Protocol/Transport Layer Security) standard for communication between web browsers and servers, as well as the WEP (Wired Equivalent Privacy) protocol that is part of the IEEE 801.11 wireless LAN standard and In the new WiFi Protected Access Protocol (WAP). Judging from these applications, RC4 forms a very important part of today's network communications, so this algorithm is very important.

Demo effect

Usage example

Next, we will introduce the usage and personalized configuration of crypto-js through specific examples.

1. Install crypto-js in the project

Install crypto-js either globally or under a module. For global installation, the installation instructions are executed in the root directory, and for module installation, the installation instructions are executed under the corresponding module:

ohpm  install @ohos/crypto-js 

For more information on OpenHarmony ohpm environment configuration and more, please refer to How to Install the OpenHarmony ohpm Package. For example, we install under the module and execute the installation instructions in the entry directory. If install completed appears, the installation is completed:

  1. Introduce dependencies

Using crypto-js requires introducing dependencies.

最新版本支持
  import {
        
         CryptoJS } from '@ohos/crypto-js' 或者
  import CryptoJS from '@ohos/crypto-js'

3. Use md5 algorithm

md5 Message-Digest Algorithm (English: md5 Message-Digest Algorithm), a widely used cryptographic hash function that can produce a 128-bit (16-byte) hash value to ensure information transmission Complete and consistent.

md5 features:

  1. Irreversibility --- The original data cannot be calculated based on the MD5 value
  2. Uniqueness --- Different original data will have different MD5 values

The use of md5 algorithm in this library:

 //第一步在需要使用到的页面,导入CryptoJS
  import {
        
         CryptoJS } from '@ohos/crypto-js'
  //第二步在需要使用到md5的业务逻辑,调用md5算法
  var hash = CryptoJS.MD5("123456") //传参是需要加密的内容,返回值是加密后的数据

  1. Use aes algorithm

The full name of the AES algorithm is Advanced Encryption Standard, also known as Rijndael encryption method. It is a block encryption standard adopted by the U.S. federal government. AES is symmetric encryption, so the same secret key is required for encryption and decryption. The use of AES algorithm in this library:

 //第一步在需要使用到的页面,导入CryptoJS
  import {
        
         CryptoJS } from '@ohos/crypto-js'
  //第二步定义加密解密需要用到的key
  var key = 'secret key 1234'
  //第三步在需要使用AES加密的业务逻辑,调用AES加密
  var encrypted = CryptoJS.AES.encrypt('hello world', key).toString()  //传参为加密内容及秘钥
  //第四步在需要把上面的加密块解密的业务逻辑,调用AES解密,注意key必须相同
  var decrypted = CryptoJS.AES.decrypt(encrypted, key) //传参为加密后的内容及秘钥

Source code address & ohpm warehouse address

The above is some basic introduction to crypto-js. For more details, you can go to the source code address or ohpm warehouse address to refer to the crypto-js documentation.

References

Install the OpenHarmony ohpm package

OpenHarmony_har_usage.md · OpenHarmony-TPC/docs - Gitee.com

Source code address

OpenHarmony-SIG/crypto-js

ohpm warehouse address

OpenHarmony three-party library central warehouse

Guess you like

Origin blog.csdn.net/OpenHarmony_dev/article/details/132754896