@exodus/bintoken

中文文档
0.1.2发布于 2023/03/15UNLICENSEDCommonJS零依赖近 30 天下载 3.1万

命名的固定长度缓冲器的二进制编码.

中文使用文档

宾托肯

Bintoken 是一个固定长度的编码二进制消息,意在作为可以签名的信息. 根据类型字节,以及谁在宾托肯上签名,消息可能具有不同的含义.

例如,如果该类型表示消息是认证符,而宾托肯由服务器的私人密钥签名,这可能会提供一种保证,即服务器在宾托肯指定的时间验证出一些信息.

符号的编码格式通过即时 BintokenEncoding 此软件包导出类别。 编码用每个属性的属性名称和长度来指定. 密钥名称在编码和解码宾托肯时按字母顺序排序.

决定二进制格式简化了签名验证. 它的固定长度简化了解码和验证,从而减少了恶意剥削的机会. 与JSON等其他格式不同,只有 一个 在宾托肯编码方案中编码消息的方式.

以下是任何宾托肯编码的通用序列化格式:

  • 类型(1字节) : 可用于识别信件上下文的版本编号.
  • [... 负载属性]
  • 时间(4个字节):创建消息时的时间戳(以unix秒计),编码为大端的uint32.

示例

以下是一个宾托肯编码的例子,它只将32字节的公钥编码为有效载荷:

const BintokenEncoding = require('@exodus/bintoken')

const tokenEncoding = new BintokenEncoding({ publicKey: 32, hash: 20 })

const token = tokenEncoding.toBuffer({
  type: 1,
  publicKey: randomBytes(32),
  hash: randomBytes(20),
})

/*
  // Erroneous use
  const token = tokenEncoding.toBuffer({ type: 1, publicKey: randomBytes(32), hash: randomBytes(20), otherProp: Buffer.alloc(0) })
  const token = tokenEncoding.toBuffer({ type: 1, publicKey: randomBytes(31), hash: randomBytes(20) })
  const token = tokenEncoding.toBuffer({ type: 1, publicKey: 'foo', hash: randomBytes(20) })
  const token = tokenEncoding.toBuffer({ type: 1 })
*/

const { type, time, publicKey, hash } = tokenEncoding.fromBuffer(token)

/*
  // Error, wrong length
  const { type, time, publicKey } = tokenEncoding.fromBuffer(
    Buffer.concat([token, Buffer.alloc(1)])
  )
*/

在这个例子中,序列化格式是:

  • 类型(1字节) : 可用于识别信件上下文的版本编号.
  • hash (20) 字节 : 消息具有意义(如类型所暗示的) 的散列.
  • public key (32字节) : 消息具有意义(由类型所暗示)的公钥.
  • 时间(4个字节):信息创建时的时间戳(以秒为单位),编码为大端的uint32.

注意到 hash 写入 在此之前 publicKey 在有效载荷中,因为属性在被写入或读出宾托肯之前按字母顺序排序。 这意味着 您为您信件选择的密钥必须使用相同的宾肯对不同的应用程序相同.

版本与发布记录

@exodus/bintoken 共发布 3 个版本,首次发布于 2021/05/26;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.2latest2023/03/153 年前
0.1.12021/05/265 年前
0.1.02021/05/265 年前

关于 @exodus/bintoken 的常见问题

@exodus/bintoken 是什么?

命名的固定长度缓冲器的二进制编码.

如何安装 @exodus/bintoken?

在项目目录运行 npm install @exodus/bintoken,或使用 pnpm add / yarn add / bun add。

@exodus/bintoken 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/exodus__bintoken 是否存在。

@exodus/bintoken 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@exodus/bintoken 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=12.18.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@exodus/bintoken 可以商用吗?

@exodus/bintoken 使用 UNLICENSED 许可。作者声明为 UNLICENSED,即不授予公开使用许可(与公共领域的 Unlicense 不同)。使用前需获得作者授权。以上为概括,不构成法律意见,请以许可原文为准。

@exodus/bintoken 的下载量有多少?

根据 npm 官方下载统计,@exodus/bintoken 在 2026/10/14 – 2026/11/12 这 30 天内被下载 31,021 次,日均约 1,034 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@exodus/bintoken 还在维护吗?

最新版本 0.1.2 发布于 2023/03/15(3 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/05/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。