KeyNub License Dongle — Gleam Package
import gleam/option.{None}
import gleam/result
import keynub/licdongle
pub fn unlock(sealed: BitArray) -> Result(BitArray, licdongle.DongleError) {
use d <- licdongle.with_dongle(None) // first dongle, or Some("serial")
use _ <- result.try(licdongle.verify_genuine(d)) // Error unless genuine
use <- licdongle.with_session(d) // closed on every exit path
licdongle.app_decrypt(d, sealed) // <- build the licence check on this
}
Nothing linked. Typed Gleam over the
keynub_licdongle Hex package, whose
small NIF loads the SDK’s native library at run time, so nothing sits in the
path of the check that a customer could substitute. Erlang target; needs Elixir
installed to build that dependency, and a C compiler for its NIF.
Setup
gleam add keynub_licdongle_gleam
The package finds keynub_licdongle_flat for your platform in natives/<platform>/
of the KeyNub SDK from the working
directory upwards. To ship an application, put the library from the SDK’s
natives folder
where it runs, or name it before the first call:
let assert Ok(Nil) = licdongle.set_library_path("/opt/keynub/libkeynub_licdongle_flat.so")
KEYNUB_LICDONGLE_FLAT_LIBRARY in the environment does the same. A process
loads the library once; loaded_library_path() tells which. On Linux, install
the udev rule described in
NATIVES.md
so the dongle is accessible without root.
Notes
- Every call returns a
Result.CallErrorcarries thestatus(NoDevice,NotGenuine,AuthRequired, …), the rawcode, theoperationand the library’sdetailtext;LibraryErrorreports a library that cannot be loaded. - Results are records (
Info,Genuine,Device,DongleRecord); byte data isBitArray. with_dongleandwith_sessionclose the dongle and the session on every exit path, crashes included, and return what the body returns. They suituse.is_genuineis the boolean form for a gate and fails closed: every failure givesFalse.erase_all_recordsis separate fromerase_record(name): an accidentally empty name must not wipe the dongle.
Read
docs/integration-security.mdbefore writing the check. Acase licdongle.is_genuine(d)is one conditional branch, and patching one of those in a release is a beginner exercise. Route something the program needs throughapp_encryptandapp_decrypt, so removing the check removes the data.
Tests
gleam test runs the unit tests. gleam run -m standin runs without a dongle:
it compiles a stand-in for the flat C API (bindings/flat/licd_flat.c over
bindings/julia/test/stub/licd_stub.c) with the C compiler on the path and
exercises every call against it. KEYNUB_SDK_ROOT names the SDK sources when
the package is not inside a clone; KEYNUB_LICDONGLE_FLAT_LIBRARY names a
compiled stand-in instead. The samples are a Gleam project under
samples/gleam (gleam run -m verify_and_read).
Links
- KeyNub License Dongle for Gleam: the product, and how to order one
- Source, samples and issue tracker on GitHub
- Native library for your platform