package com.fs.qw.utils;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.util.Base64;


public class RSASignatureUtils {
    public static String sign(String data, PrivateKey privateKey) throws Exception {
        Signature signature = Signature.getInstance("SHA256withRSA");
        signature.initSign(privateKey);
        signature.update(data.getBytes());
        byte[] signed = signature.sign();
        return Base64.getEncoder().encodeToString(signed); // 返回Base64编码的签名
    }

    public static boolean verify(String data, String signature, PublicKey publicKey) throws Exception {
        byte[] signedBytes = Base64.getDecoder().decode(signature);
        Signature verifier = Signature.getInstance("SHA256withRSA");
        verifier.initVerify(publicKey);
        verifier.update(data.getBytes());
        return verifier.verify(signedBytes);
    }
}
