diff --git a/CHANGELOG.md b/CHANGELOG.md index 53c9716b9ed4fc02b470c0c62383eafde5c5185d..8f3be31da57daeabd3c6a645d35f783d6307f099 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## v1.0.3 + +1. 修复加密算法中特殊字符以及中文字符加密错误的问题 + ## v1.0.2 1. 适配API9 diff --git a/README.md b/README.md index 2f42b045879f0c129d8a1e3b11277bd1bbac7da4..3a1e4b76921ae9702dc55a28a819d80884f40b5d 100644 --- a/README.md +++ b/README.md @@ -1,200 +1,115 @@ # crypto-js ## 简介 - -> crypto-js是一个加密算法类库,可以非常方便的进行加解密操作。目前crypto-js已支持的算法有:MD5、SHA-1、SHA-256、HMAC、HMAC-MD5、HMAC-SHA1、HMAC-SHA256、 -> PBKDF2、AES、RC4、DES等等。 +> crypto-js是一个加密算法类库,可以非常方便的在前端进行其所支持的加解密操作。目前crypto-js已支持的算法有:MD5、SHA-1、SHA-256、HMAC、HMAC-MD5、HMAC-SHA1、HMAC-SHA256、PBKDF2等。 ![preview.gif](preview/preview.gif) ## 下载安装 - ```shell npm install @ohos/crypto-js --save ``` - OpenHarmony npm环境配置等更多内容,请参考 [如何安装OpenHarmony npm包](https://gitee.com/openharmony-tpc/docs/blob/master/OpenHarmony_npm_usage.md) 。 ## 使用说明 - -### 引入依赖 - +1. 引入依赖 ``` import { CryptoJS } from '@ohos/crypto-js' ``` - -### Message Digests - -- md5 - +2. md5加密 ``` CryptoJS.MD5('message') ``` - -- sha1 - +3. sha1加密 ``` CryptoJS.SHA1('message'); ``` - -- sha256 - +4. sha256加密 ``` CryptoJS.SHA256('message') ``` - -- sha512 - +5. sha512加密 ``` CryptoJS.SHA512('message') ``` - -- ripemd160 - +6. ripemd160加密 ``` CryptoJS.RIPEMD160('message') ``` - -- hmac-md5 - +7. hmac-md5加密 ``` CryptoJS.HmacMD5('message', 'pwd') ``` - -- hmac-sha1 - +8. hmac-sha1加密 ``` CryptoJS.HmacSHA1('message', 'pwd') ``` - -- hmac-sha256 - +9. hmac-sha256加密 ``` CryptoJS.HmacSHA256('message', 'pwd') ``` - -- hmac-sha512 - +10. hmac-sha512加密 ``` CryptoJS.HmacSHA512('message', 'pwd') ``` - -- hmac-ripemd160 - +11. hmac-ripemd160加密 ``` CryptoJS.HmacRIPEMD160('message', 'pwd') ``` - -### Ciphers - -支持AES、DES、RC4、RABBIT的加密和解密功能,以及操作模式:CBC、CFB、CTR、CTRGladman、OFB和ECB。 - -- AES - -加密 - +12. WordArray随机数 ``` - CryptoJS.AES.encrypt(message, CryptoJS.enc.Utf8.parse(key), { - iv: CryptoJS.enc.Utf8.parse(iv), - mode: CryptoJS.mode.CBC, - padding: CryptoJS.pad.Pkcs7 - }).toString(); +CryptoJS.lib.WordArray.random(nBytes) //nBytes表示要生成的随机字节数。 ``` - -输出的密文默认是base64编码的,如果需要输出的是hex,可做如下设置: +13. hex 十六进制编码 ``` - CryptoJS.AES.encrypt(message, CryptoJS.enc.Utf8.parse(key), { - iv: CryptoJS.enc.Utf8.parse(iv), - mode: CryptoJS.mode.CBC, - padding: CryptoJS.pad.Pkcs7, - format: CryptoJS.format.Hex - }).toString(); + let wordArray = CryptoJS.format.Hex.parse(hexStr) //字符串转换为wordArray + let result = CryptoJS.format.Hex.stringify(wordArray) //将wordArray转换为十六进制字符串 ``` - -解密 - -默认对base64编码的密文进行解密,如果你的密文是hex的,需要如上一样的设置。 +14. aes加解密 ``` - CryptoJS.AES.decrypt(ciphertext, CryptoJS.enc.Utf8.parse(key), { - iv: CryptoJS.enc.Utf8.parse(iv), - mode: CryptoJS.mode.CBC, - padding: CryptoJS.pad.Pkcs7 - }).toString(CryptoJS.enc.Utf8); + var encrypted = CryptoJS.AES.encrypt('hello world', 'secret key 1234').toString() //传参为加密内容及秘钥 + var decrypted = CryptoJS.AES.decrypt(encrypted, 'secret key 1234') //传参为加密后的内容及秘钥 ``` -- DES - -支持在ECB和CBC模式下提供Triple DES(3DES)和DES加密和解密。 - -加密 +15. des加解密 ``` - CryptoJS.DES.encrypt(message, CryptoJS.enc.Utf8.parse(key), { - mode: CryptoJS.mode.ECB, - padding: CryptoJS.pad.Pkcs7 - }).toString(); + let encrypted = CryptoJS.DES.encrypt('hello', 'secret key 123').toString() //传参加密内容及秘钥 + let decrypted = CryptoJS.DES.decrypt(CryptoJS.DES.encrypt('hello', 'secret key 123').toString(), 'secret key 123') //传参为加密后的内容及秘钥 ``` -解密 - -``` -CryptoJS.DES.decrypt(ciphertext, CryptoJS.enc.Utf8.parse(key), { - mode: CryptoJS.mode.ECB, - padding: CryptoJS.pad.Pkcs7 - }).toString(CryptoJS.enc.Utf8); -``` - -- 其他加解密算法的使用方式与上面方式相同。 - -### Utilities - -- base64 +16. base64 编码 - ``` - let wordArray=CryptoJS.enc.Utf8.parse(message); - let result=CryptoJS.enc.Base64.stringify(wordArray); - ``` - -解码 - ``` - CryptoJS.enc.Base64.parse(result).toString(CryptoJS.enc.Utf8); + let wordArray=CryptoJS.enc.Utf8.parse(message); + let result=CryptoJS.enc.Base64.stringify(wordArray); ``` -- hex -编码 - ``` - let wordArray =CryptoJS.enc.Utf8.parse(message); - let result = CryptoJS.enc.Hex.stringify(wordArray) - ``` 解码 + ``` - CryptoJS.enc.Hex.parse(result).toString(CryptoJS.enc.Utf8); + CryptoJS.enc.Base64.parse(result); ``` -- 随机数 - ``` - CryptoJS.lib.WordArray.random(nBytes) - ``` -### 针对文件使用说明 -将文件数据TypedArray(支持Uint8Array、Uint16Array、Uint32Array三种)转化为WordArray再进行加密处理。 -- md5 +## 针对文件使用说明 + +1. md5 ``` Crypto.MD5(CryptoJs.lib.WordArray.create(array)) ``` -- sha1 +2. sha1 ``` Crypto.SHA1(CryptoJs.lib.WordArray.create(array)) ``` -- sha256 +3. sha256 ``` Crypto.SHA256(CryptoJs.lib.WordArray.create(array)) @@ -202,80 +117,76 @@ CryptoJS.DES.decrypt(ciphertext, CryptoJS.enc.Utf8.parse(key), { -## 接口说明 -接口列表: -- ```crypto-js/md5``` -- ```crypto-js/sha1``` -- ```crypto-js/sha256``` -- ```crypto-js/sha224``` -- ```crypto-js/sha512``` -- ```crypto-js/sha384``` -- ```crypto-js/sha3``` -- ```crypto-js/ripemd160``` +## 接口列表 ---- +- `crypto-js/md5` +- `crypto-js/sha1` +- `crypto-js/sha256` +- `crypto-js/sha224` +- `crypto-js/sha512` +- `crypto-js/sha384` +- `crypto-js/sha3` +- `crypto-js/ripemd160` +------ -- ```crypto-js/hmac-md5``` -- ```crypto-js/hmac-sha1``` -- ```crypto-js/hmac-sha256``` -- ```crypto-js/hmac-sha224``` -- ```crypto-js/hmac-sha512``` -- ```crypto-js/hmac-sha384``` -- ```crypto-js/hmac-sha3``` -- ```crypto-js/hmac-ripemd160``` +- `crypto-js/hmac-md5` +- `crypto-js/hmac-sha1` +- `crypto-js/hmac-sha256` +- `crypto-js/hmac-sha224` +- `crypto-js/hmac-sha512` +- `crypto-js/hmac-sha384` +- `crypto-js/hmac-sha3` +- `crypto-js/hmac-ripemd160` ---- +------ -- ```crypto-js/pbkdf2``` +- `crypto-js/pbkdf2` ---- +------ -- ```crypto-js/aes``` -- ```crypto-js/tripledes``` -- ```crypto-js/rc4``` -- ```crypto-js/rabbit``` -- ```crypto-js/rabbit-legacy``` -- ```crypto-js/evpkdf``` -- ```crypto-js/des``` -- ```crypto-js/triple-des``` +- `crypto-js/aes` +- `crypto-js/tripledes` +- `crypto-js/rc4` +- `crypto-js/rabbit` +- `crypto-js/rabbit-legacy` +- `crypto-js/evpkdf` +- `crypto-js/des` +- `crypto-js/triple-des` ---- +------ -- ```crypto-js/format-openssl``` -- ```crypto-js/format-hex``` +- `crypto-js/format-openssl` +- `crypto-js/format-hex` ---- +------ -- ```crypto-js/enc-latin1``` -- ```crypto-js/enc-utf8``` -- ```crypto-js/enc-hex``` -- ```crypto-js/enc-utf16``` -- ```crypto-js/enc-base64``` +- `crypto-js/enc-latin1` +- `crypto-js/enc-utf8` +- `crypto-js/enc-hex` +- `crypto-js/enc-utf16` +- `crypto-js/enc-base64` ---- +------ -- ```crypto-js/mode-cfb``` -- ```crypto-js/mode-ctr``` -- ```crypto-js/mode-ctr-gladman``` -- ```crypto-js/mode-ofb``` -- ```crypto-js/mode-ecb``` +- `crypto-js/mode-cfb` +- `crypto-js/mode-ctr` +- `crypto-js/mode-ctr-gladman` +- `crypto-js/mode-ofb` +- `crypto-js/mode-ecb` ---- - -- ```crypto-js/pad-pkcs7``` -- ```crypto-js/pad-ansix923``` -- ```crypto-js/pad-iso10126``` -- ```crypto-js/pad-iso97971``` -- ```crypto-js/pad-zeropadding``` -- ```crypto-js/pad-nopadding``` +------ +- `crypto-js/pad-pkcs7` +- `crypto-js/pad-ansix923` +- `crypto-js/pad-iso10126` +- `crypto-js/pad-iso97971` +- `crypto-js/pad-zeropadding` +- `crypto-js/pad-nopadding` ## 兼容性 - 支持 OpenHarmony API version 9 版本。 ## 目录结构 - ```` |---- crypto-js | |---- entry # 示例代码文件夹 @@ -289,10 +200,7 @@ CryptoJS.DES.decrypt(ciphertext, CryptoJS.enc.Utf8.parse(key), { ```` ## 贡献代码 - -使用过程中发现任何问题都可以提 [Issue](https://gitee.com/openharmony-sig/crypto-js/issues) -给我们,当然,我们也非常欢迎你给我们发 [PR](https://gitee.com/openharmony-sig/crypto-js/pulls) 。 +使用过程中发现任何问题都可以提 [Issue](https://gitee.com/openharmony-sig/crypto-js/issues) 给我们,当然,我们也非常欢迎你给我们发 [PR](https://gitee.com/openharmony-sig/crypto-js/pulls) 。 ## 开源协议 - 本项目基于 [MIT License](https://gitee.com/openharmony-sig/crypto-js/blob/master/LICENSE) ,请自由地享受和参与开源。 diff --git a/crypto/package.json b/crypto/package.json index 18222718cfcc191d20685176b97bc8fc51a5f833..7142d79755d68059913bd3a223b8db770d80730b 100644 --- a/crypto/package.json +++ b/crypto/package.json @@ -11,7 +11,7 @@ }, "main": "index.ets", "repository": "https://gitee.com/openharmony-sig/crypto-js", - "version": "1.0.2", + "version": "1.0.3", "tags": [ "crypto-js", "OpenHarmony" diff --git a/crypto/src/main/ets/crypto.ts b/crypto/src/main/ets/crypto.ts index d964133357d5136bad051d6167ef27239397e98a..467e65f86ba9e8ccff707626640385f17a18b1d0 100644 --- a/crypto/src/main/ets/crypto.ts +++ b/crypto/src/main/ets/crypto.ts @@ -13,6 +13,7 @@ * limitations under the License. */ +//@ts-nocheck import CryptoJS from 'crypto-js'; export { CryptoJS } @@ -51,6 +52,79 @@ CryptoJS.enc.Utf8.stringify = function (wordArray) { } } -CryptoJS.enc.Utf8.parse = function (utf8Str) { - return CryptoJS.enc.Latin1.parse(encodeURI(utf8Str)); +const { + StringPrototypeCharCodeAt +} = primordials; +const { Buffer } = require('buffer'); + +const unhexTable = new Int8Array([ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0 - 15 + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 16 - 31 + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 32 - 47 + +0, +1, +2, +3, +4, +5, +6, +7, +8, +9, -1, -1, -1, -1, -1, -1, // 48 - 63 + -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 64 - 79 + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 80 - 95 + -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 96 - 111 + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 112 - 127 + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 128 ... + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 255 +]); +/** + * A safe fast alternative to decodeURIComponent + * @param {string} s + * @param {boolean} decodeSpaces + * @returns {string} + */ +function unescapeBuffer(s, decodeSpaces) { + const out = Buffer.allocUnsafe(s.length); + let index = 0; + let outIndex = 0; + let currentChar; + let nextChar; + let hexHigh; + let hexLow; + const maxLength = s.length - 2; + // Flag to know if some hex chars have been decoded + let hasHex = false; + while (index < s.length) { + currentChar = StringPrototypeCharCodeAt(s, index); + if (currentChar === 43 /* '+' */ + && decodeSpaces) { + out[outIndex++] = 32; // ' ' + index++; + continue; + } + if (currentChar === 37 /* '%' */ + && index < maxLength) { + currentChar = StringPrototypeCharCodeAt(s, ++index); + hexHigh = unhexTable[currentChar]; + if (!(hexHigh >= 0)) { + out[outIndex++] = 37; // '%' + continue; + } else { + nextChar = StringPrototypeCharCodeAt(s, ++index); + hexLow = unhexTable[nextChar]; + if (!(hexLow >= 0)) { + out[outIndex++] = 37; // '%' + index--; + } else { + hasHex = true; + currentChar = hexHigh * 16 + hexLow; + } + } + } + out[outIndex++] = currentChar; + index++; + } + return hasHex ? out.slice(0, outIndex) : out; } + +CryptoJS.enc.Utf8.parse = function (utf8Str) { + return CryptoJS.enc.Latin1.parse(unescapeBuffer(encodeURIComponent(utf8Str), false).toString("latin1")); +} \ No newline at end of file