npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@datkt/sodium

v0.7.2

Published

libsodium bindings for Kotlin

Downloads

45

Readme

sodium

libsodium bindings for Kotlin/Native.

Installation

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

From NPM

$ npm install @datkt/sodium

Prerequisites

Usage

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

where main.kt might be

import datkt.sodium.* // entire libsodium API
import kotlinx.cinterop.* // exposes types needed for interop

fun main(args: Array<String>) {
  if (0 != sodium_init()) {
    throw Error("Failed to initialize libsodium")
  }
}

Example

import kotlinx.cinterop.*
import datkt.sodium.sodium_init
import datkt.sodium.randombytes_buf
import datkt.sodium.randombytes_random
import datkt.sodium.randombytes_uniform

fun main(args: Array<String>) {
  val rc = sodium_init()

  if (0 != rc) {
    throw Error("sodium_init() != 0")
  }

  println("${randombytes_random()} = randombytes_random()")
  println("${randombytes_uniform(37)} = randombytes_uniform(37)")

  var buffer = ByteArray(16)

  buffer.usePinned { pinned ->
    randombytes_buf(pinned.addressOf(0), buffer.size.convert())
  }

  println("${toHexString(buffer)} = randombytes_buf(ByteArray(16), 16)")
}

val table = "0123456789abcdef".toCharArray()

fun toHexString(bytes: ByteArray): String {
  var output = CharArray(2 * bytes.size)
  for (i in bytes.indices) {
    val j = (bytes[i].toInt() and 0xff).toInt()
    output[2 * i] = table[j ushr 4]
    output[1 + 2 * i] = table[j and 0x0f]
  }
  return String(output)
}

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:

int crypto_generichash(unsigned char *out,
                       size_t outlen,
                       const unsigned char *in,
                       unsigned long long inlen,
                       const unsigned char *key,
                       size_t keylen);

translates to the following Kotlin function signature:

fun crypto_generichash(out: CValuesRef<UByteVar>?,
                       outlen: size_t,
                       `in`: CValuesRef<UByteVar>?,
                       inlen: ULong,
                       key: CValuesRef<UByteVar>?,
                       keylen: size_t): Int;

and can be called in Kotlin like:

val buffer = ByteArray(crypto_generichash_BYTES.toInt())
val message = "hello"

buffer.usePinned { pinned ->
  crypto_generichash(
    pinned.addressOf(0) as CValuesRef<UByteVar>,
    buffer.size.convert(),
    message.cstr as CValuesRef<UByteVar>,
    message.length.convert(),
    null, // key
    0 // key size
  )
}

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.

$ 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.

$ 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

  • https://libsodium.gitbook.io/doc
  • https://github.com/jedisct1/libsodium
  • https://github.com/sodium-friends/sodium-native

License

MIT