騰訊雲>>雲通訊>>TLS後臺API在mac上JAVA DEMO搭建

1.相關文檔地址

2.相關demo代碼

代碼部分做了修改,使用了commons-io中的IOUtils.toString簡化了io操做java

public class Demo {

    public static void main(String args[]) throws Exception {

        tls_sigcheck demo = new tls_sigcheck();

        // 使用前請修改動態庫的加載路徑
        // demo.loadJniLib("D:\\src\\oicq64\\tinyid\\tls_sig_api\\windows\\64\\lib\\jni\\jnisigcheck.dll");
        demo.loadJniLib("/home/tls/tls_sig_api/src/jnisigcheck.so");

        String priKey;
        try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("ec_key.pem")) {
            priKey = IOUtils.toString(in, "utf-8");
        }
        int ret = demo.tls_gen_signature_ex2("1400000267", "xiaojun", priKey);

        if (0 != ret) {
            System.out.println("ret " + ret + " " + demo.getErrMsg());
        } else {
            System.out.println("sig:\n" + demo.getSig());
        }

        String pubKey;
        try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("public.pem")) {
            pubKey = IOUtils.toString(in, "utf-8");
        }
        ret = demo.tls_check_signature_ex2(demo.getSig(), pubKey, "1400000267", "xiaojun");
        if (0 != ret) {
            System.out.println("ret " + ret + " " + demo.getErrMsg());
        } else {
            System.out.println("--\nverify ok -- expire time " + demo.getExpireTime() + " -- init time " + demo.getInitTime());
        }
    }
}

從上述代碼能夠知道,java接口是使用jni的方式,而且在linux環境下使用了jnisigcheck.so,相關下載
微雲分享>TLS後臺API>20151230下面的tls_sig_api-linux-32.tar.gz或者tls_sig_api-linux-64.tar.gz,解壓後,在目錄lib/jni中能夠獲取。很遺憾,mac中不能直接使用,會報一下錯誤(以tls_sig_api-linux-64中的jnisigcheck.so爲例):python

Exception in thread "main" java.lang.UnsatisfiedLinkError: /home/tls/tls_sig_api/src/jnisigcheck.so: dlopen(/home/tls/tls_sig_api/src/jnisigcheck.so, 1): no suitable image found.  Did find:
    /home/tls/tls_sig_api/src/jnisigcheck.so: unknown file type, first eight bytes: 0x7F 0x45 0x4C 0x46 0x01 0x01 0x01 0x00
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1938)
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1821)
    at java.lang.Runtime.load0(Runtime.java:809)
    at java.lang.System.load(System.java:1086)
    at com.tls.sigcheck.tls_sigcheck.loadJniLib(tls_sigcheck.java:10)
    at com.xiaobenma020.main(Demo.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

所以在mac中,須要從新編譯生成jnisigcheck.solinux

3.jnisigcheck.so編譯步驟

3.1下載TLS相關源碼,下載地址

微雲分享>TLS後臺API>20151230>tls_sig_api-src.tar.gzgit

3.2 解壓tls_sig_api-src.tar.gz以後,進入src目錄,查看Makefile

CPP = g++
CC = gcc
ARCH=$(shell getconf LONG_BIT)
CFLAGS= -g -I../include -I../include/tls_sig_api -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux -Wall -fPIC
LIBS= ../linux/$(ARCH)/lib/jsoncpp/libjsoncpp.a ../openssl-dynamic/lib/libcrypto.a -ldl -lz
TARGETS = libtlsignature.a sigcheck.so signature

all: $(TARGETS)

libtlsignature.a: tls_signature.o multi_thread.o base64.o base64_url.o
    ar -rs $@ $^

# jni 庫編譯前請安裝 jdk,而且配置 JAVA_HOME 環境變量
jni: com_tls_sigcheck_tls_sigcheck.o libtlsignature.a
    g++ -shared -o jnisigcheck.so $^ ../linux/$(ARCH)/lib/jsoncpp/libjsoncpp.a ../openssl-dynamic/lib/libcrypto.a -lz

signature: signature.o libtlsignature.a
    g++ -o $@ $^ ../linux/$(ARCH)/lib/jsoncpp/libjsoncpp.a ../openssl-dynamic/lib/libcrypto.a -ldl -lz
    
sigcheck.so: sigcheck.o libtlsignature.a sigcheck.h
    g++ -shared -fPIC -o $@ $^ ../linux/$(ARCH)/lib/jsoncpp/libjsoncpp.a ../openssl-dynamic/lib/libcrypto.a -ldl -lz

.cpp.o:
    $(CPP) $(CFLAGS) -c $*.cppmkd

.c.o:
    $(CC) $(CFLAGS) -c $*.c

clean:  
    -rm -f *.o *.so *.a tls_licence_tools TAGS $(TARGETS)

能夠看到使用make jni生成jnisigcheck.so。執行命令以後,並無像咱們想象那樣生成了jnisigcheck.so,須要解決部分依賴庫問題:jni_md.h,代碼中的../linux/$(ARCH)/lib/jsoncpp/libjsoncpp.a ($(ARCH)=64)../openssl-dynamic/lib/libcrypto.a文件缺失問題。github

3.3解決文件缺失問題

3.3.1 複製jni_md.hshell

cd /Library/Java/JavaVirtualMachines/jdk1.8.0_73.jdk/Contents/Home/include/ sudo darwin/jni_md.h ./json

3.3.2 編譯jsoncpptls_sig_api-src/jsoncpp-src-0.5.0)windows

參考Linux下編譯Jsoncppapi

//請認真讀jsoncpp-src-0.5.0/README.txtapp

$ brew install scons
$ cd jsoncpp-src-0.5.0
$ python /usr/local/bin/scons platform=linux-gcc

執行的結果是在libs目錄下生成了

linux-gcc-4.2.1/libjson_linux-gcc-4.2.1_libmt.a
linux-gcc-4.2.1/libjson_linux-gcc-4.2.1_libmt.dylib

3.3.3 編譯openssl

參考build_openssl_dylib.sh

進入tls_sig_api-src目錄

$ tar -xzvf openssl-1.0.2a.tar.gz
$ mv openssl-1.0.2a openssl_x86_64
$ cd openssl_x86_64
$ ./Configure darwin64-x86_64-cc -shared
$ make

則在當前目錄生成了libcrypto.a

3.3.4 連接依賴庫

進入tls_sig_api-src/src

$ mkdir -p ../linux/64/lib/jsoncpp
$ ln -s `cd ..;pwd`/jsoncpp-src-0.5.0/libs/linux-gcc-4.2.1/libjson_linux-gcc-4.2.1_libmt.a ../linux/64/lib/jsoncpp/libjsoncpp.a
$ mkdir -p ../openssl-dynamic/lib/
$ ln -s `cd ..;pwd`/openssl_x86_64/libcrypto.a ../openssl-dynamic/lib/libcrypto.a

建立link的時候,source須要填寫絕對路徑,因此纔有上述cd ..;pwd

3.4 編譯jnisigcheck.so

在src目錄下,執行一下命令,則生成`jnisigcheck.so`
$ make clean
$ make jni
相關文章
相關標籤/搜索