In this post, I build a shared library and dynamic library by a Go source code and use it for spreadsheet decryption. A Shared Object/Library is a binary file with a dynamic/static loading table for functions.
Go language Source Code
// decrypt.go
package main
import "C"
import (
"fmt"
"github.com/xuri/excelize/v2"
)
//export decryption
func decryption(file *C.char, passwd *C.char) {
f, err := excelize.OpenFile(C.GoString(file), excelize.Options{Password: C.GoString(passwd)})
if err != nil {
return
}
defer func() {
if err := f.Close(); err != nil {
fmt.Println(err)
}
}()
if err := f.SaveAs("decrypted workbook.xlsx", excelize.Options{}); err != nil {
fmt.Println(err)
}
}
func main() {
}
Build shared libraries
Build a dynamic shared library
go build -o libdecrypt.so -buildmode=c-shared decrypt.go
It will generate two files: libdecrypt.a and libdecrypt.h.
Build a static shared library
go build -o libdecrypt.a -buildmode=c-archive
It will generate two files: libdecrypt.so and libdecrypt.h.
Using a dynamic shared library in C++ program
// decrypt.cpp
#include <stdio.h>
#include <unistd.h>
#include "libdecrypt.h"
int main() {
decryption((char*)"encryptAES.xlsx", (char*)"password");
}
Build a executable file:
gcc -v decrypt.cpp -o decrypt ./libdecrypt.so
By executing the following command we can get decrypted spreadsheet: decrypted workbook.xlsx.
./decrypt
Using a static shared library in C program
// decrypt.c
#include "libdecrypt.h"
int main ()
{
decryption("encryptAES.xlsx", "password");
}
Build a executable file:
gcc -o decrypt decrypt.c libdecrypt.a -lpthread
By executing the following command we can get decrypted spreadsheet.
./decrypt
That explains how to build the dynamic and static shared library (shared object) in Go, and how to use them.
The open-source project excelize-py is a practice for more complex scenario such as complex custom data structure.