| 68 | * @returns {boolean|string} 加密成功返回明文,加密失败返回false |
| 69 | */ |
| 70 | export function AESGCMEncrypt (key, iv, msg) { |
| 71 | msg = util.encodeUtf8(msg); |
| 72 | const cipherInstance = cipher.createCipher('AES-GCM', key); |
| 73 | cipherInstance.start({ |
| 74 | iv: iv, |
| 75 | additionalData: '', // 'binary-encoded string', // optional |
| 76 | tagLength: 128 // optional, defaults to 128 bits |
| 77 | }); |
| 78 | cipherInstance.update(util.createBuffer(msg)); |
| 79 | const pass = cipherInstance.finish(); |
| 80 | if (pass) { |
| 81 | const encrypted = cipherInstance.output; |
| 82 | const tag = cipherInstance.mode.tag; |
| 83 | return window.btoa(encrypted.data + tag.data); |
| 84 | } |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @private |