项目作者: datkt

项目描述 :
libsodium bindings for Kotlin/Native
高级语言: Shell
项目地址: git://github.com/datkt/sodium.git
创建时间: 2018-10-11T17:37:48Z
项目社区:https://github.com/datkt/sodium

开源协议:MIT License

下载


sodium

libsodium bindings for Kotlin/Native.

Installation

The datkt.sodium package an be installed with various package managers.

From NPM

  1. $ npm install @datkt/sodium

Prerequisites

Usage

  1. ## Compile a program in 'main.kt' and link sodium.klib found in `node_modules/`
  2. $ konanc -r node_modules/@datkt -l sodium/sodium main.kt

where main.kt might be

  1. import datkt.sodium.* // entire libsodium API
  2. import kotlinx.cinterop.* // exposes types needed for interop
  3. fun main(args: Array<String>) {
  4. if (0 != sodium_init()) {
  5. throw Error("Failed to initialize libsodium")
  6. }
  7. }
  8. `

Example

  1. import kotlinx.cinterop.*
  2. import datkt.sodium.sodium_init
  3. import datkt.sodium.randombytes_buf
  4. import datkt.sodium.randombytes_random
  5. import datkt.sodium.randombytes_uniform
  6. fun main(args: Array<String>) {
  7. val rc = sodium_init()
  8. if (0 != rc) {
  9. throw Error("sodium_init() != 0")
  10. }
  11. println("${randombytes_random()} = randombytes_random()")
  12. println("${randombytes_uniform(37)} = randombytes_uniform(37)")
  13. var buffer = ByteArray(16)
  14. buffer.usePinned { pinned ->
  15. randombytes_buf(pinned.addressOf(0), buffer.size.convert())
  16. }
  17. println("${toHexString(buffer)} = randombytes_buf(ByteArray(16), 16)")
  18. }
  19. val table = "0123456789abcdef".toCharArray()
  20. fun toHexString(bytes: ByteArray): String {
  21. var output = CharArray(2 * bytes.size)
  22. for (i in bytes.indices) {
  23. val j = (bytes[i].toInt() and 0xff).toInt()
  24. output[2 * i] = table[j ushr 4]
  25. output[1 + 2 * i] = table[j and 0x0f]
  26. }
  27. return String(output)
  28. }

libsodium API

This package binds libsodiums entire API and provides an
interop
API for Kotlin and can be imported from the sodium package. For
example, the crypto_generichash() with the following C function
signature:

  1. int crypto_generichash(unsigned char *out,
  2. size_t outlen,
  3. const unsigned char *in,
  4. unsigned long long inlen,
  5. const unsigned char *key,
  6. size_t keylen);

translates to the following Kotlin function signature:

  1. fun crypto_generichash(out: CValuesRef<UByteVar>?,
  2. outlen: size_t,
  3. `in`: CValuesRef<UByteVar>?,
  4. inlen: ULong,
  5. key: CValuesRef<UByteVar>?,
  6. keylen: size_t): Int;

and can be called in Kotlin like:

  1. val buffer = ByteArray(crypto_generichash_BYTES.toInt())
  2. val message = "hello"
  3. buffer.usePinned { pinned ->
  4. crypto_generichash(
  5. pinned.addressOf(0) as CValuesRef<UByteVar>,
  6. buffer.size.convert(),
  7. message.cstr as CValuesRef<UByteVar>,
  8. message.length.convert(),
  9. null, // key
  10. 0 // key size
  11. )
  12. }

Building

The sodium package can be built from source into various targets.

Kotlin Library

sodium.klib, a Kotlin library that can be linked with konanc can be
built from source.

  1. $ make klib

which will produce build/lib/sodium.klib. The library can be installed
with klib by running make install

Static Library

libsodium.a, a static library that can be linked with konanc can be
built from source.

  1. $ make static

which will produce build/lib/libsodium.a and C header files in
build/include. The library can be installed into your system by
running make install. The path prefix can be set by defining the
PREFIX environment or make variable. It defaults to
PREFIX=/usr/local

See Also

License

MIT