This repository has been archived on 2022-10-07. You can view files and clone it, but cannot push or open issues or pull requests.
skynet-webportal/docker/nginx/libs/skynet/skylink.lua

41 lines
1.1 KiB
Lua
Raw Normal View History

2021-11-05 11:57:06 +00:00
local _M = {}
2021-11-05 14:32:32 +00:00
local basexx = require("basexx")
2021-12-20 10:27:29 +00:00
local hasher = require("hasher")
2021-11-05 11:57:06 +00:00
-- parse any skylink and return base64 version
function _M.parse(skylink)
if string.len(skylink) == 55 then
2021-11-05 14:32:32 +00:00
local decoded = basexx.from_basexx(string.upper(skylink), "0123456789ABCDEFGHIJKLMNOPQRSTUV", 5)
2021-11-05 11:57:06 +00:00
2021-11-05 14:32:32 +00:00
return basexx.to_url64(decoded)
2021-11-05 11:57:06 +00:00
end
return skylink
end
2021-12-20 10:27:29 +00:00
-- hash skylink into 32 bytes hash used in blocklist
function _M.hash(skylink)
-- ensure that the skylink is base64 encoded
local base64Skylink = _M.parse(skylink)
-- decode skylink from base64 encoding
local rawSkylink = basexx.from_url64(base64Skylink)
-- drop first two bytes and leave just merkle root
local rawMerkleRoot = string.sub(rawSkylink, 3)
-- parse with blake2b with key length of 32
local blake2bHashed = hasher.blake2b(rawMerkleRoot, 32)
-- hex encode the blake hash
local hexHashed = basexx.to_hex(blake2bHashed)
-- lowercase the hex encoded hash
local lowerHexHashed = string.lower(hexHashed)
return lowerHexHashed
end
2021-11-05 11:57:06 +00:00
return _M