• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 package com.ohos.hapsigntool.utils;
17 
18 import com.ohos.hapsigntool.hap.sign.SignatureAlgorithm;
19 import org.apache.logging.log4j.LogManager;
20 import org.apache.logging.log4j.Logger;
21 
22 import java.io.File;
23 import java.io.IOException;
24 import java.nio.file.Files;
25 import java.util.Arrays;
26 import java.util.HashSet;
27 import java.util.Set;
28 
29 /**
30  * Utils functions for processing parameters.
31  *
32  * @since 2021/12/21
33  */
34 public class ParamProcessUtil {
ParamProcessUtil()35     private ParamProcessUtil() {
36     }
37 
38     private static final Logger LOGGER = LogManager.getLogger(ParamProcessUtil.class);
39 
40     /**
41      * Use string array to init string set
42      *
43      * @param paramFileds input string array
44      * @return string set.
45      */
initParamField(String[] paramFileds)46     public static Set<String> initParamField(String[] paramFileds) {
47         return new HashSet<String>(Arrays.asList(paramFileds));
48     }
49 
50     /**
51      * Delete all files Recursively
52      *
53      * @param file file path.
54      */
delDir(File file)55     public static void delDir(File file) {
56         if (file.isDirectory()) {
57             File[] zFiles = file.listFiles();
58             if (zFiles != null) {
59                 for (File file2 : zFiles) {
60                     delDir(file2);
61                 }
62             }
63         }
64         try {
65             Files.delete(file.toPath());
66         } catch (IOException e) {
67             LOGGER.warn("delete files failed!");
68         }
69     }
70 
71     /**
72      * Get SignatureAlgorithm value by algorithm name.
73      *
74      * @param signatureAlgorithm algorithm name.
75      * @return SignatureAlgorithm value
76      */
getSignatureAlgorithm(String signatureAlgorithm)77     public static SignatureAlgorithm getSignatureAlgorithm(String signatureAlgorithm) {
78         SignatureAlgorithm result;
79         if (ParamConstants.HAP_SIG_ALGORITHM_SHA256_ECDSA.equalsIgnoreCase(signatureAlgorithm)) {
80             result = SignatureAlgorithm.ECDSA_WITH_SHA256;
81         } else if (ParamConstants.HAP_SIG_ALGORITHM_SHA384_ECDSA.equalsIgnoreCase(signatureAlgorithm)) {
82             result = SignatureAlgorithm.ECDSA_WITH_SHA384;
83         } else if (ParamConstants.HAP_SIG_ALGORITHM_SHA512_ECDSA.equalsIgnoreCase(signatureAlgorithm)) {
84             result = SignatureAlgorithm.ECDSA_WITH_SHA512;
85         } else if (ParamConstants.HAP_SIG_ALGORITHM_SHA256_RSA_PSS.equalsIgnoreCase(signatureAlgorithm)) {
86             result = SignatureAlgorithm.RSA_PSS_WITH_SHA256;
87         } else if (ParamConstants.HAP_SIG_ALGORITHM_SHA384_RSA_PSS.equalsIgnoreCase(signatureAlgorithm)) {
88             result = SignatureAlgorithm.RSA_PSS_WITH_SHA384;
89         } else if (ParamConstants.HAP_SIG_ALGORITHM_SHA512_RSA_PSS.equalsIgnoreCase(signatureAlgorithm)) {
90             result = SignatureAlgorithm.RSA_PSS_WITH_SHA512;
91         } else if (ParamConstants.HAP_SIG_ALGORITHM_SHA256_RSA_MGF1.equalsIgnoreCase(signatureAlgorithm)) {
92             result = SignatureAlgorithm.RSA_PSS_WITH_SHA256;
93         } else if (ParamConstants.HAP_SIG_ALGORITHM_SHA384_RSA_MGF1.equalsIgnoreCase(signatureAlgorithm)) {
94             result = SignatureAlgorithm.RSA_PSS_WITH_SHA384;
95         } else if (ParamConstants.HAP_SIG_ALGORITHM_SHA512_RSA_MGF1.equalsIgnoreCase(signatureAlgorithm)) {
96             result = SignatureAlgorithm.RSA_PSS_WITH_SHA512;
97         } else {
98             throw new IllegalArgumentException("Unsupported signature algorithm: " + signatureAlgorithm);
99         }
100         return result;
101     }
102 }
103