
com.webauthn4j.util.RSAUtil Maven / Gradle / Ivy
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.webauthn4j.util;
import com.webauthn4j.util.exception.UnexpectedCheckedException;
import org.jetbrains.annotations.NotNull;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAKeyGenParameterSpec;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
public class RSAUtil {
private RSAUtil() {
}
public static @NotNull PublicKey createPublicKey(@NotNull RSAPublicKeySpec rsaPublicKeySpec) {
try {
KeyFactory factory = KeyFactory.getInstance("RSA");
return factory.generatePublic(rsaPublicKeySpec);
} catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
throw new UnexpectedCheckedException(e);
}
}
public static @NotNull PrivateKey createPrivateKey(@NotNull RSAPrivateKeySpec rsaPrivateKeySpec) {
try {
KeyFactory factory = KeyFactory.getInstance("RSA");
return factory.generatePrivate(rsaPrivateKeySpec);
} catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
throw new UnexpectedCheckedException(e);
}
}
public static @NotNull KeyPair createKeyPair() {
KeyPairGenerator keyPairGenerator;
try {
keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(new RSAKeyGenParameterSpec(2048, RSAKeyGenParameterSpec.F4), new SecureRandom());
} catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException e) {
throw new UnexpectedCheckedException(e);
}
return keyPairGenerator.generateKeyPair();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy