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