
ru.tinkoff.acquiring.sdk.CryptoUtils Maven / Gradle / Ivy
/*
* Copyright © 2016 Tinkoff Bank
*
* 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
*
* http://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 ru.tinkoff.acquiring.sdk;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import ru.tinkoff.acquiring.sdk.utils.Base64;
/**
* @author Mikhail Artemyev
*/
public class CryptoUtils {
public static String sha256(final String string) {
if (string == null) {
throw new IllegalArgumentException("Argument cannot be null");
}
try {
final MessageDigest digest = MessageDigest.getInstance("SHA-256");
final byte[] hash = digest.digest(string.getBytes("UTF-8"));
final StringBuffer hexString = new StringBuffer();
for (int i = 0; i < hash.length; i++) {
final String hex = Integer.toHexString(0xff & hash[i]);
if (hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
return hexString.toString();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
public static byte[] encryptRsa(final String string, final PublicKey publicKey) {
try {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(string.getBytes());
} catch (NoSuchPaddingException | InvalidKeyException | NoSuchAlgorithmException |
BadPaddingException | IllegalBlockSizeException e) {
throw new RuntimeException(e);
}
}
public static String encodeBase64(final byte[] value) {
return Base64.encodeToString(value, Base64.DEFAULT).trim();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy