# datom-crypto

> cryto tools or functions using in datoms protocol.

Latest version **1.3.0** (published 2022-08-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install datom-crypto
pnpm add datom-crypto
yarn add datom-crypto
bun add datom-crypto
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.0 |
| Published | 2022-08-20 |
| First published | 2022-03-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 8.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | jerry.zhang |
| Maintainers | jerry.zhang.bill |

## Links

- npm: https://www.npmjs.com/package/datom-crypto
- npm.io page: https://npm.io/package/datom-crypto

## Dependencies (5)

- [b4a](https://npm.io/package/b4a.md) ^1.1.0
- [jws](https://npm.io/package/jws.md) ^4.0.0
- [chalk](https://npm.io/package/chalk.md) ^5.0.1
- [compact-encoding](https://npm.io/package/compact-encoding.md) ^2.5.1
- [sodium-universal](https://npm.io/package/sodium-universal.md) ^3.0.0

## Recent versions

- 1.3.0 (latest) — 2022-08-20
- 1.2.0 — 2022-03-26
- 1.1.0 — 2022-03-12
- 1.0.0 — 2022-03-11

## README

# datom-crypto
datom协议中，需要用到的涉及加密的方法和函数。包括非对称加密、对称加密以及JWS。

```js
npm install datom-crypto
```

## 用法

```js
const crypto = require('datom-crypto')

//生成一对新的keyPair: {keyPair.PublicKey, keyPair.secretKey}

const keyPair = crypto.keyPair()
  
console.log('publicKey is ',keyPair.publicKey.toString('hex'))
console.log('secretKey is',keyPair.secretKey.toString('hex'))


//用keyPair生成的secretkey密钥对message进行签名
const message = b4a.from('hello world')
const sig = crypto.sign(message, keyPair.secretKey)
console.log('sign message using mySecretKey is', sig.toString('hex'))


//对JSON格式的数据进行签名
rawdata = {
  Id01:{
  'name':'jerry zhang',
  'banks':'ICBC',
  'account':'28289x-229',
  'balance': '72882973'
  },
  Id02:{
  'name':'Chris zhang',
  'banks':'IBC',
  'account':'128289x-229',
  'balance': '728s82973'
  },
  Id03:{
  'name':'jerry zhang',
  'banks':'ICBC',
  'account':'28289x-229',
  'balance': '72882973'
  }
}

const sign_jws = jws.sign({
  header: { alg: 'HS256' },  //参照JWS规范
  payload: rawdata,  
  secret: keyPair.secretKey,
});

console.log('JWS sign is :\n',sign_jws)

```

## API 
```js
keyPair = crypto.keyPair()
```
返回一个ED25519的keypair，用于后续merkle Tree的签名。

```js
signature = crypto.sign(message, secretKey)
```
用secretKey对message (buffer)签名.

```js
verified = crypto.verify(message, signature, publicKey)
```
对message，验证其签名.

```js
hash = crypto.data(data)
```
对merkle tree的一个node进行hash.

```js
hash = crypto.parent(left, right)
```
对merkle tree的一个父节点hash。树的左、右节点格式要求如下：

```js
{
  index: treeIndex,
  hash: hashOfThisNode,
  size: byteSizeOfThisTree
}
```

```js
hash = crypto.tree(peaks)
```
merkle tree的根节点的hash. 

```js
buffer = crypto.randomBytes(size)
```
返回一个特定大小的,包含随机bytes的buffer.

```js
hash = crypto.discoveryKey(publicKey)
```
从keyPair.publicKey计算出discoveryKey，而无需直接暴露discoverKey.

```js
list = crypto.namespace(name, count)
```
从一个特定公开的name生成一个namespace的清单。

---
_Source: https://npm.io/package/datom-crypto · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
