|
1 |
| -# GmSSL-JNI |
2 |
| -GmSSL Java Wrapper |
| 1 | +# GmSSL JNI |
| 2 | + |
| 3 | +本项目是GmSSL密码库接口的Java语言封装,可以用于Java及Android平台上的应用开发。GmSSL JNI提供了包括随机数生成、对称加解密、哈希、消息认证码(MAC)、公钥加解密、数字签名、密钥交换等基础密码功能的Java语言接口,支持包括SM2/SM3/SM4/ZUC在内的GmSSL密码库的主要密码算法。 |
| 4 | + |
| 5 | +## 接口说明 |
| 6 | + |
| 7 | +GmSSL Java Wrapper的接口如下: |
| 8 | + |
| 9 | +```java |
| 10 | +public class GmSSL { |
| 11 | + |
| 12 | + public native String getVersion(int type); |
| 13 | + public native byte [] generateRandom(int length); |
| 14 | + public native String [] getCiphers(boolean aliases); |
| 15 | + public native int getCipherIVLength(String cipher); |
| 16 | + public native int getCipherKeyLength(String cipher); |
| 17 | + public native int getCipherBlockSize(String cipher); |
| 18 | + public native byte [] symmetricEncrypt(String cipher, int flag, byte [] in, byte [] key, byte [] iv); |
| 19 | + public native byte [] symmetricDecrypt(String cipher, int flag, byte [] in, byte [] key, byte [] iv); |
| 20 | + public native String [] getDigests(boolean aliases); |
| 21 | + public native int getDigestLength(String digestAlgor); |
| 22 | + public native int getDigestBlockSize(String digestAlgor); |
| 23 | + public native byte [] digest(String algor, int flag, byte [] data); |
| 24 | + public native String [] getMacs(boolean aliases); |
| 25 | + public native String [] getMacLength(String algor); |
| 26 | + public native byte [] mac(String algor, int flag, byte [] data, byte [] key); |
| 27 | + public native String [] getSignAlgorithms(boolean aliases); |
| 28 | + public native byte [] sign(String algor, int flag, byte [] data, byte [] privateKey); |
| 29 | + public native int verify(String algor, int flag, byte [] digest, byte [] signature, byte [] publicKey); |
| 30 | + public native String [] getPublicKeyEncryptions(boolean aliases); |
| 31 | + public native byte [] publicKeyEncrypt(String algor, int flag, byte [] in, byte [] publicKey); |
| 32 | + public native byte [] publicKeyDecrypt(String algor, int falg, byte [] in, byte [] privateKey); |
| 33 | + public native String [] getDeriveKeyAlgorithms(boolean aliases); |
| 34 | + public native byte [] deriveKey(String algor, int flag, int keyLength, byte [] peerPublicKey, byte [] privateKey); |
| 35 | + |
| 36 | + static { |
| 37 | + System.loadLibrary("gmssl"); |
| 38 | + } |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +GmSSL-JNI的实现依赖链接的GmSSL库,应用程序在运行时可以通过`getVersion()`接口获取GmSSL库的版本号。通过`getCiphers()`、`getDigests()`、`getMacs()`、`getSignAlgorithms()`、`getPublicKeyEncryptions()`和`getKeyDeriveAlgorithms()`获取当前GmSSL支持的算法,这些接口返回以`:`字符分隔的算法名称字符串,这些算法名称字符串可以用于密码操作接口的输入参数。在GmSSL中,部分算法有别名,通过`aliases`参数可以设定是否输出算法别名。 |
| 43 | + |
| 44 | +GmSSL Java Wrapper支持如下密码功能: |
| 45 | + |
| 46 | +* 随机数生成:`generateRandom()` |
| 47 | +* 对称加解密:`symmetricEncrypt()`、`symmetricDecrypt()` |
| 48 | +* 哈希:`digest()` |
| 49 | +* 消息认证码:`mac()` |
| 50 | +* 数字签名:`sign()`、`verify()` |
| 51 | +* 公钥加解密:`publicKeyEncrypt()`、`publicKeyDecrypt()` |
| 52 | +* 密钥交换:`deriveKey()` |
| 53 | + |
| 54 | +除了随机数生成之外,其他的接口都需要提供字符串格式的算法名称。 |
| 55 | + |
| 56 | +### 返回值 |
| 57 | + |
| 58 | +应用应该总是检查返回值。 |
| 59 | + |
| 60 | +* 如果返回值为整数,仅当返回值为大于0时正确,小于等于0时错误。 |
| 61 | +* 如果返回值为字符串或字节数组,返回空表示错误 |
| 62 | + |
0 commit comments