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.entity; 17 18 import java.security.spec.AlgorithmParameterSpec; 19 import java.security.spec.MGF1ParameterSpec; 20 import java.security.spec.PSSParameterSpec; 21 22 /** 23 * Signature algorithm 24 * 25 * @since 2021-12-13 26 */ 27 public enum SignatureAlgorithm { 28 RSA_PSS_WITH_SHA256( 29 0x101, 30 "RSA", 31 ContentDigestAlgorithm.SHA256, 32 Pair.create( 33 "SHA256withRSAANDMGF1", 34 new PSSParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA256, 256 / 8, 1))), 35 RSA_PSS_WITH_SHA384( 36 0x102, 37 "RSA", 38 ContentDigestAlgorithm.SHA384, 39 Pair.create( 40 "SHA384withRSAANDMGF1", 41 new PSSParameterSpec("SHA-384", "MGF1", MGF1ParameterSpec.SHA384, 384 / 8, 1))), 42 RSA_PSS_WITH_SHA512( 43 0x103, 44 "RSA", 45 ContentDigestAlgorithm.SHA512, 46 Pair.create( 47 "SHA512withRSAANDMGF1", 48 new PSSParameterSpec("SHA-512", "MGF1", MGF1ParameterSpec.SHA512, 512 / 8, 1))), 49 RSA_PKCS1_V1_5_WITH_SHA256(0x104, "RSA", ContentDigestAlgorithm.SHA256, Pair.create("SHA256withRSA", null)), 50 RSA_PKCS1_V1_5_WITH_SHA384(0x105, "RSA", ContentDigestAlgorithm.SHA256, Pair.create("SHA384withRSA", null)), 51 RSA_PKCS1_V1_5_WITH_SHA512(0x106, "RSA", ContentDigestAlgorithm.SHA512, Pair.create("SHA512withRSA", null)), 52 ECDSA_WITH_SHA256(0x201, "EC", ContentDigestAlgorithm.SHA256, Pair.create("SHA256withECDSA", null)), 53 ECDSA_WITH_SHA384(0x202, "EC", ContentDigestAlgorithm.SHA384, Pair.create("SHA384withECDSA", null)), 54 ECDSA_WITH_SHA512(0x203, "EC", ContentDigestAlgorithm.SHA512, Pair.create("SHA512withECDSA", null)), 55 DSA_WITH_SHA256(0x301, "DSA", ContentDigestAlgorithm.SHA256, Pair.create("SHA256withDSA", null)), 56 DSA_WITH_SHA384(0x302, "DSA", ContentDigestAlgorithm.SHA384, Pair.create("SHA384withDSA", null)), 57 DSA_WITH_SHA512(0x303, "DSA", ContentDigestAlgorithm.SHA512, Pair.create("SHA512withDSA", null)); 58 59 private int id; 60 61 private String keyAlgorithm; 62 63 private ContentDigestAlgorithm contentDigestAlgorithm; 64 65 private Pair<String, ? extends AlgorithmParameterSpec> signatureAlgAndParams; 66 SignatureAlgorithm( int id, String keyAlgorithm, ContentDigestAlgorithm contentDigestAlgorithm, Pair<String, ? extends AlgorithmParameterSpec> signatureAlgAndParams)67 SignatureAlgorithm( 68 int id, 69 String keyAlgorithm, 70 ContentDigestAlgorithm contentDigestAlgorithm, 71 Pair<String, ? extends AlgorithmParameterSpec> signatureAlgAndParams) { 72 this.id = id; 73 this.keyAlgorithm = keyAlgorithm; 74 this.contentDigestAlgorithm = contentDigestAlgorithm; 75 this.signatureAlgAndParams = signatureAlgAndParams; 76 } 77 getId()78 public int getId() { 79 return id; 80 } 81 getKeyAlgorithm()82 public String getKeyAlgorithm() { 83 return keyAlgorithm; 84 } 85 getContentDigestAlgorithm()86 public ContentDigestAlgorithm getContentDigestAlgorithm() { 87 return contentDigestAlgorithm; 88 } 89 getSignatureAlgAndParams()90 public Pair<String, ? extends AlgorithmParameterSpec> getSignatureAlgAndParams() { 91 return signatureAlgAndParams; 92 } 93 94 /** 95 * Find value of SignatureAlgorithm according to ID 96 * 97 * @param id ID of SignatureAlgorithm object 98 * @return SignatureAlgorithm object 99 */ findById(int id)100 public static SignatureAlgorithm findById(int id) { 101 SignatureAlgorithm ret = null; 102 for (SignatureAlgorithm alg : SignatureAlgorithm.values()) { 103 if (id == alg.getId()) { 104 return alg; 105 } 106 } 107 return ret; 108 } 109 } 110