1/* 2 * Copyright (c) 2020 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16/** 17 * @devices smartVision 18 */ 19export interface CipherResponse { 20 /** 21 * response text 22 * @devices smartVision 23 */ 24 text: string; 25} 26 27/** 28 * @devices smartVision 29 */ 30export interface CipherRsaOptions { 31 /** 32 * Action type. 33 * The options are as follows: 34 * encrypt: Encrypts data. 35 * decrypt: Decrypts data. 36 * @devices smartVision 37 * @since 3 38 */ 39 action: string; 40 41 /** 42 * Text content to be encrypted or decrypted. 43 * The text to be encrypted must be a common text and cannot exceed the length calculated based on the formula (keySize/8 - 66). 44 * keySize indicates the key length. 45 * For example, if the key length is 1024 bytes, the text cannot exceed 62 bytes (1024/8 - 66 = 62). 46 * The text content to be decrypted must be a binary value encoded using Base64. 47 * The default format is used for Base64 encoding. 48 * @devices smartVision 49 * @since 3 50 */ 51 text: string; 52 53 /** 54 * Keys encrypted using RSA. 55 * During encryption, this parameter is a public key. 56 * During decryption, it is a private key. 57 * @devices smartVision 58 * @since 3 59 */ 60 key: string; 61 62 /** 63 * RSA algorithm padding. 64 * The default value is RSA/None/OAEPWithSHA256AndMGF1Padding. 65 * @devices smartVision 66 * @since 3 67 */ 68 transformation?: string; 69 70 /** 71 * Called when data is encrypted or decrypted successfully. 72 * @devices smartVision 73 * @since 3 74 */ 75 success: (data: CipherResponse) => void; 76 77 /** 78 * Called when data fails to be encrypted or decrypted. 79 * @devices smartVision 80 * @since 3 81 */ 82 fail: (data: string, code: number) => void; 83 84 /** 85 * Called when the execution is completed. 86 * @devices smartVision 87 * @since 3 88 */ 89 complete: () => void; 90} 91 92/** 93 * @devices smartVision 94 */ 95export interface CipherAesOptions { 96 /** 97 * Action type. 98 * The options are as follows: 99 * encrypt: Encrypts data. 100 * decrypt: Decrypts data. 101 * @devices smartVision 102 * @since 3 103 */ 104 action: string; 105 106 /** 107 * Text content to be encrypted or decrypted. 108 * The text to be encrypted must be a common text. 109 * The text content to be decrypted must be a binary value encoded using Base64. 110 * The default format is used for Base64 encoding. 111 * @devices smartVision 112 * @since 3 113 */ 114 text: string; 115 116 /** 117 * Key used for encryption or decryption, which is a character string encrypted using Base64. 118 * @devices smartVision 119 * @since 3 120 */ 121 key: string; 122 123 /** 124 * Encryption mode and padding of the AES algorithm. 125 * The default value is AES/CBC/PKCS5Padding. 126 * @devices smartVision 127 * @since 3 128 */ 129 transformation?: string; 130 131 /** 132 * Initial vector for AES-based encryption and decryption. 133 * The value is a character string encoded using Base64. 134 * The default value is the key value. 135 * @devices smartVision 136 * @since 3 137 */ 138 iv?: string; 139 140 /** 141 * Offset of the initial vector for AES-based encryption and decryption. 142 * The default value is 0. 143 * @devices smartVision 144 * @since 3 145 */ 146 ivOffset?: string; 147 148 /** 149 * Length of the initial vector for AES-based encryption and decryption. 150 * The default value is 16. 151 * @devices smartVision 152 * @since 3 153 */ 154 ivLen?: string; 155 156 /** 157 * Called when data is encrypted or decrypted successfully. 158 * @devices smartVision 159 * @since 3 160 */ 161 success: (data: CipherResponse) => void; 162 163 /** 164 * Called when data fails to be encrypted or decrypted. 165 * @devices smartVision 166 * @since 3 167 */ 168 fail: (data: string, code: number) => void; 169 170 /** 171 * Called when the execution is completed. 172 * @devices smartVision 173 * @since 3 174 */ 175 complete: () => void; 176} 177 178/** 179 * @devices smartVision 180 */ 181export default class Cipher { 182 /** 183 * Encrypts or decrypts data using RSA. 184 * @param options RSA options 185 * @devices smartVision 186 */ 187 static rsa(options: CipherRsaOptions): void; 188 189 /** 190 * Encrypts or decrypts data using AES. 191 * @param options AES options 192 * @devices smartVision 193 */ 194 static aes(options: CipherAesOptions): void; 195} 196