1/*
2 * Copyright (c) 2023-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
16import CheckEmptyUtils from '@ohos/common';
17
18export default class CommonUtils {
19
20  private static readonly two: number = 2;
21  private static readonly four: number = 4;
22  private static readonly asterisk: string = '***';
23
24  /**
25   * convert String to return a SecureString half_substring
26   */
27  public static getSecurityMac(str: string): string {
28    if (str === undefined || str === null || str.length === 0) {
29      return '';
30    }
31    if (str.length < CommonUtils.two) {
32      return CommonUtils.asterisk;
33    }
34    return str.substring(0, CommonUtils.two) + CommonUtils.asterisk + str.substring(str.length - CommonUtils.two, str.length);
35  }
36
37  /**
38  * convert String to return a SecureString
39  */
40  public static getSecurityIp(str: string): string {
41    if (str === undefined || str === null || str.length === 0) {
42      return '';
43    }
44
45    return str.substring(0, str.length / CommonUtils.two).toString() + CommonUtils.asterisk;
46  }
47
48  /**
49  * convert String to return a SecureString
50  */
51  public static getSecurityPath(str: string): string {
52    if (str === undefined || str === null || str.length === 0) {
53      return '';
54    }
55    let split: string[] = str.split('/');
56    if (CheckEmptyUtils.isEmptyArr(split)) {
57      return '';
58    }
59    return split[split.length - 1];
60  }
61
62  /**
63  * convert String to return a SecureString
64  */
65  public static getSecurityPrinterName(str: string): string {
66    if (str === undefined || str === null || str.length === 0) {
67      return '';
68    }
69    if (str.length < CommonUtils.four) {
70      return CommonUtils.asterisk;
71    }
72    return str.substring(0, CommonUtils.four) + CommonUtils.asterisk + str.substring(str.length - CommonUtils.four, str.length);
73  }
74}