1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.apksig.internal.apk.v1; 18 19 import java.util.Comparator; 20 21 /** 22 * Digest algorithm used with JAR signing (aka v1 signing scheme). 23 */ 24 public enum DigestAlgorithm { 25 /** SHA-1 */ 26 SHA1("SHA-1"), 27 28 /** SHA2-256 */ 29 SHA256("SHA-256"); 30 31 private final String mJcaMessageDigestAlgorithm; 32 DigestAlgorithm(String jcaMessageDigestAlgoritm)33 private DigestAlgorithm(String jcaMessageDigestAlgoritm) { 34 mJcaMessageDigestAlgorithm = jcaMessageDigestAlgoritm; 35 } 36 37 /** 38 * Returns the {@link java.security.MessageDigest} algorithm represented by this digest 39 * algorithm. 40 */ getJcaMessageDigestAlgorithm()41 String getJcaMessageDigestAlgorithm() { 42 return mJcaMessageDigestAlgorithm; 43 } 44 45 public static Comparator<DigestAlgorithm> BY_STRENGTH_COMPARATOR = new StrengthComparator(); 46 47 private static class StrengthComparator implements Comparator<DigestAlgorithm> { 48 @Override compare(DigestAlgorithm a1, DigestAlgorithm a2)49 public int compare(DigestAlgorithm a1, DigestAlgorithm a2) { 50 switch (a1) { 51 case SHA1: 52 switch (a2) { 53 case SHA1: 54 return 0; 55 case SHA256: 56 return -1; 57 } 58 throw new RuntimeException("Unsupported algorithm: " + a2); 59 60 case SHA256: 61 switch (a2) { 62 case SHA1: 63 return 1; 64 case SHA256: 65 return 0; 66 } 67 throw new RuntimeException("Unsupported algorithm: " + a2); 68 69 default: 70 throw new RuntimeException("Unsupported algorithm: " + a1); 71 } 72 } 73 } 74 } 75