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