001// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org> 002// 003// SPDX-License-Identifier: Apache-2.0 004 005package org.pgpainless.key.util; 006 007import java.math.BigInteger; 008import java.util.regex.Pattern; 009 010public final class KeyIdUtil { 011 012 private KeyIdUtil() { 013 014 } 015 016 private static final Pattern LONG_KEY_ID = Pattern.compile("^[0-9A-Fa-f]{16}$"); 017 018 /** 019 * Convert a long key-id into a key-id. 020 * A long key-id is a 16 digit hex string. 021 * 022 * @param longKeyId 16-digit hexadecimal string 023 * @return key-id converted to {@link Long}. 024 */ 025 public static long fromLongKeyId(String longKeyId) { 026 if (!LONG_KEY_ID.matcher(longKeyId).matches()) { 027 throw new IllegalArgumentException("Provided long key-id does not match expected format. " + 028 "A long key-id consists of 16 hexadecimal characters."); 029 } 030 031 return new BigInteger(longKeyId, 16).longValue(); 032 } 033}