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