DSA安全编码组件 @author SeanDragon
| 26 | * @author SeanDragon |
| 27 | */ |
| 28 | public final class ToolDSA { |
| 29 | private ToolDSA() { |
| 30 | throw new UnsupportedOperationException("我是工具类,别初始化我。。。"); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * 数字签名密钥算法 |
| 35 | */ |
| 36 | public static final String ALGORITHM = "DSA"; |
| 37 | |
| 38 | /** |
| 39 | * 数字签名 签名/验证算法 |
| 40 | */ |
| 41 | public static final String SIGNATURE_ALGORITHM = "SHA1withDSA"; |
| 42 | |
| 43 | /** |
| 44 | * 公钥 |
| 45 | */ |
| 46 | private static final String PUBLIC_KEY = "DSAPublicKey"; |
| 47 | |
| 48 | /** |
| 49 | * 私钥 |
| 50 | */ |
| 51 | private static final String PRIVATE_KEY = "DSAPrivateKey"; |
| 52 | |
| 53 | /** |
| 54 | * DSA密钥长度 默认1024位, 密钥长度必须是64的倍数, 范围在512至1024位之间(含) |
| 55 | */ |
| 56 | private static final int KEY_SIZE = 1024; |
| 57 | |
| 58 | /** |
| 59 | * 签名 |
| 60 | * |
| 61 | * @param data |
| 62 | * 待签名数据 |
| 63 | * @param privateKey |
| 64 | * 私钥 |
| 65 | * |
| 66 | * @return byte[] 数字签名 |
| 67 | * |
| 68 | * @throws Exception |
| 69 | */ |
| 70 | public static byte[] sign(byte[] data, byte[] privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, SignatureException { |
| 71 | // 还原私钥 |
| 72 | // 转换私钥材料 |
| 73 | PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKey); |
| 74 | |
| 75 | // 实例化密钥工厂 |
| 76 | KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM); |
| 77 | |
| 78 | // 生成私钥对象 |
| 79 | PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec); |
| 80 | |
| 81 | // 实例化Signature |
| 82 | Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); |
| 83 | |
| 84 | // 初始化Signature |
| 85 | signature.initSign(priKey); |
nothing calls this directly
no outgoing calls
no test coverage detected