Crimpua is an implementation of Crimp in Lua.
Please see the bbc-news/crimp repo for more details.
Crimpua can be installed with Luarocks from GitHub:
luarocks install https://raw.githubusercontent.com/BBC-News/crimpua/v1.0.0-0/crimp-1.0.0-0.rockspec
local Crimp = require("crimp")
print(Crimp.signature({a = {b = 1}})) -- prints "ac13c15d07e5fa3992fc6b15113db900"
Lua doesn't distinguish between an array and a hash. They're both tables, but an array is indexed with numbers.
letters = {'a', 'b', 'c', 'd'}
print(letters) -- prints "table: 0x2119ef0"
print(letters[1]) -- prints "a"
We distinguish between an array and a hash by inspecting table[1]
which returns the first element for an array and nil for a hash.
You'll see why we have mentioned the use of tables in the next section.
Under the hood Crimp annotates the passed data structure to a nested array of primitives (strings, numbers, booleans, nils, etc.) and a single byte to indicate the type of the primitive:
Type | Byte |
---|---|
String | S |
Number | N |
Boolean | B |
nil | _ |
Array | A |
Hash | H |
You can verify it using the #notation
function:
Crimp.notation({a=1})
"1NaSAH"
Before signing Crimpua, uses the #notation
function to transform the return value of a table to a string. However the terminal strips away quotes when using LuaJIT (Just-In-Time-Compiler).
Crimp.notation({ a: { b: 'c' } })
"aSbScSAHAH"
Please note the Arrays and Hash keys are sorted before signing.
Crimp.notation({3, 1, 2})
"1N2N3NA"
key/value tuples get sorted as well.
Crimp.notation({ b: 3 })
"3NbSAH"
Initial release.
For testing we use busted.
luarocks install crimp-1.0.0-0.rockspec --only-deps
luarocks test