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 * Defines the cipher response. 18 * @since 3 19 * @syscap SystemCapability.Security.Cipher 20 * @permission N/A 21 */ 22export interface CipherResponse { 23 /** 24 * response text 25 * @since 3 26 * @syscap SystemCapability.Security.Cipher 27 */ 28 text: string; 29} 30 31/** 32 * Defines the rsa cipher options. 33 * @since 3 34 * @syscap SystemCapability.Security.Cipher 35 * @permission N/A 36 */ 37export interface CipherRsaOptions { 38 /** 39 * Action type. 40 * The options are as follows: 41 * encrypt: Encrypts data. 42 * decrypt: Decrypts data. 43 * @since 3 44 * @syscap SystemCapability.Security.Cipher 45 */ 46 action: string; 47 48 /** 49 * Text content to be encrypted or decrypted. 50 * The text to be encrypted must be a common text and cannot exceed the length calculated based on the formula (keySize/8 - 66). 51 * keySize indicates the key length. 52 * For example, if the key length is 1024 bytes, the text cannot exceed 62 bytes (1024/8 - 66 = 62). 53 * The text content to be decrypted must be a binary value encoded using Base64. 54 * The default format is used for Base64 encoding. 55 * @since 3 56 * @syscap SystemCapability.Security.Cipher 57 */ 58 text: string; 59 60 /** 61 * Keys encrypted using RSA. 62 * During encryption, this parameter is a public key. 63 * During decryption, it is a private key. 64 * @since 3 65 * @syscap SystemCapability.Security.Cipher 66 */ 67 key: string; 68 69 /** 70 * RSA algorithm padding. 71 * The default value is RSA/None/OAEPWithSHA256AndMGF1Padding. 72 * @since 3 73 * @syscap SystemCapability.Security.Cipher 74 */ 75 transformation?: string; 76 77 /** 78 * Called when data is encrypted or decrypted successfully. 79 * @since 3 80 * @syscap SystemCapability.Security.Cipher 81 */ 82 success: (data: CipherResponse) => void; 83 84 /** 85 * Called when data fails to be encrypted or decrypted. 86 * @since 3 87 * @syscap SystemCapability.Security.Cipher 88 */ 89 fail: (data: string, code: number) => void; 90 91 /** 92 * Called when the execution is completed. 93 * @since 3 94 * @syscap SystemCapability.Security.Cipher 95 */ 96 complete: () => void; 97} 98 99/** 100 * Defines the aes cipher options. 101 * @since 3 102 * @syscap SystemCapability.Security.Cipher 103 * @permission N/A 104 */ 105export interface CipherAesOptions { 106 /** 107 * Action type. 108 * The options are as follows: 109 * encrypt: Encrypts data. 110 * decrypt: Decrypts data. 111 * @syscap SystemCapability.Security.Cipher 112 * @since 3 113 */ 114 action: string; 115 116 /** 117 * Text content to be encrypted or decrypted. 118 * The text to be encrypted must be a common text. 119 * The text content to be decrypted must be a binary value encoded using Base64. 120 * The default format is used for Base64 encoding. 121 * @syscap SystemCapability.Security.Cipher 122 * @since 3 123 */ 124 text: string; 125 126 /** 127 * Key used for encryption or decryption, which is a character string encrypted using Base64. 128 * @syscap SystemCapability.Security.Cipher 129 * @since 3 130 */ 131 key: string; 132 133 /** 134 * Encryption mode and padding of the AES algorithm. 135 * The default value is AES/CBC/PKCS5Padding. 136 * @syscap SystemCapability.Security.Cipher 137 * @since 3 138 */ 139 transformation?: string; 140 141 /** 142 * Initial vector for AES-based encryption and decryption. 143 * The value is a character string encoded using Base64. 144 * The default value is the key value. 145 * @syscap SystemCapability.Security.Cipher 146 * @since 3 147 */ 148 iv?: string; 149 150 /** 151 * Offset of the initial vector for AES-based encryption and decryption. 152 * The default value is 0. 153 * @syscap SystemCapability.Security.Cipher 154 * @since 3 155 */ 156 ivOffset?: string; 157 158 /** 159 * Length of the initial vector for AES-based encryption and decryption. 160 * The default value is 16. 161 * @syscap SystemCapability.Security.Cipher 162 * @since 3 163 */ 164 ivLen?: string; 165 166 /** 167 * Called when data is encrypted or decrypted successfully. 168 * @syscap SystemCapability.Security.Cipher 169 * @since 3 170 */ 171 success: (data: CipherResponse) => void; 172 173 /** 174 * Called when data fails to be encrypted or decrypted. 175 * @syscap SystemCapability.Security.Cipher 176 * @since 3 177 */ 178 fail: (data: string, code: number) => void; 179 180 /** 181 * Called when the execution is completed. 182 * @syscap SystemCapability.Security.Cipher 183 * @since 3 184 */ 185 complete: () => void; 186} 187 188/** 189 * Defines the cipher functions. 190 * @since 3 191 * @deprecated since 9 192 * @useinstead ohos.security.cryptoFramework.Cipher 193 * @syscap SystemCapability.Security.Cipher 194 * @permission N/A 195 */ 196export default class Cipher { 197 /** 198 * Encrypts or decrypts data using RSA. 199 * @deprecated since 9 200 * @param options RSA options 201 * @syscap SystemCapability.Security.Cipher 202 */ 203 static rsa(options: CipherRsaOptions): void; 204 205 /** 206 * Encrypts or decrypts data using AES. 207 * @deprecated since 9 208 * @param options AES options 209 * @syscap SystemCapability.Security.Cipher 210 */ 211 static aes(options: CipherAesOptions): void; 212} 213