密码工具
在浏览器或节点中使用加密密钥工作的功能.
这是一些有用的功能,可以更容易地与密码学合作. 注意这一点 没有 处理存储密钥。 查看使用 @ 自行车编码/ webauthn 键 (生物计量认证)或 索引数据库 帮帮忙.
Webcrypto 键比较可取,因为我们创建它们作为 不可提取 键,并能够在索引数据库中坚持它们,尽管无法读取私钥.
[TIP] 请求使用
.persist()方法.
[注] 安装大小有点大(9.77 MB),因为这包括一个精密的捆绑 钠库。 。 。 .
再说, 参见从打字符生成的文件
目录
- 安装
- 实例
- API 密码
- 网络crypto API
- 网络crypto AES API
- webcrypto RSA API 页面存档备份,存于互联网档案馆
- eCC API 网络催眠
- aPI 钠
- AES API钠
- ECC API钠
- 另见
安装
npm i -S @bicycle-codes/crypto-util
实例
创建新密钥页
使用带有 ECC 密钥 网络加密 API。 。 。 .
还有 你可以用 RSA 密钥。 。 。 .
import { create, KeyUse } from '@bicycle-codes/crypto-util'
// create a new keypair
const encryptKeypair = await create(KeyUse.Encrypt)
const signKeys = await create(KeyUse.Sign)
使用 2 ECC 键盘创建新的 AES 键
这需要一个 keypair + 另一个 keypair 来获取一个共享的 AES 密钥 .
import { getSharedKey } from '@bicycle-codes/crypto-util/webcrypto/ecc'
import { KeyUse } from '@bicycle-codes/crypto-util/types'
const alicesKeys = await createEcc(KeyUse.Encrypt)
const bobsKeys = await createEcc(KeyUse.Encrypt)
// pass in our private key, their public key
const sharedKey = await getSharedKey(alicesKeys.privateKey, bobsKeys.publicKey)
Bob可以通过使用他们的私钥+爱丽丝的公钥来得到相同的密钥.
const bobsSharedKey = await getSharedKey(bobsKeys.privateKey, alicesKeys.publicKey)
用 AES 密钥加密
用给定的密钥加密给定的消息 .
import { create, encrypt } from '@bicycle-codes/crypto-util/webcrypto/aes'
const aesKey = await create()
const aesEncryptedText = await encrypt('hello AES', aesKey)
用 AES 密钥解密
import { decrypt } from '@bicycle-codes/crypto-util/webcrypto/aes'
const decrypted = await decrypt(aesEncryptedText, aesKey)
用ECC 密钥加密
这是爱丽丝给鲍勃的信 我们用爱丽丝的私人钥匙和鲍勃的 公用钥匙.
import { KeyUse } from '@bicycle-codes/crypto-util'
import {
create,
encrypt,
decrypt
} from '@bicycle-codes/crypto-util/webcrypto'
const alicesKeys = await create(KeyUse.Encrypt)
const bobsKeys = await create(KeyUse.Encrypt)
const eccEncryptedText = await encrypt(
'hello ecc',
alicesKeys.privateKey,
bobsKeys.publicKey
)
用ECC 密钥解密
鲍勃可以解密爱丽丝加密的信息,因为我们使用了Bob的公开 加密时键.
// note keys are reversed here --
// alice's public key and bob's private key
const decrypted = await decrypt(
eccEncryptedText,
bobsKeys.privateKey,
alicesKeys.publicKey
)
// => 'hello ecc'
创建签名密钥包
创建另一个用于签名的密钥派 .
import { KeyUse } from '@bicycle-codes/crypto-util'
import { create } from '@bicycle-codes/crypto-util/webcrypto'
const eccSignKeys = await create(KeyUse.Sign)
创建签名
import { sign } from '@bicycle-codes/crypto-util/webcrypto/ecc'
const sig = await sign('hello dids', eccSignKeys.privateKey)
创建 DD
ADD是一个 分散化标识符,字符串编码用户的公钥.
如果您正在传送您的公钥和信件, 例如, 这是首选格式 .
import { publicKeyToDid } from '@bicycle-codes/crypto-util'
const did = await publicKeyToDid(eccSignKeys.publicKey)
验证签名
使用 DDE 来验证签名字符串 .
import { verifyWithDid, sign } from '@bicycle-codes/crypto-util/ecc'
const sig = await sign('hello dids', eccSignKeys.privateKey)
const isOk = await verifyWithDid('hello dids', sig, did)
API 密码
这暴露了无害环境管理和共同的联署材料 软件包.json exports 字段。 。 。 .
无害环境管理
import * as util from '@bicycle-codes/crypto-utils'
共同联合来文
const util = require('@bicycle-codes/crypto-utils')
预建联署材料
这个包也暴露了精密的,预先捆绑的JS文件. 复制到一个位置 然后在 HTML 中链接.
复制
cp ./node_modules/@bicycle-codes/crypto-util/dist/index.min.js ./public/crypto-util.js
HTML 语言
<script type="module" src="./crypto-util.js"></script>
网络催眠对钠
用来使用 网络crypto API,从 webcrypto 潜行道.
网络crypto API
这取决于环境与 网络crypto API。 。 。 .
import { aes, ecc, rsa } from '@bicycle-codes/crypto-util/webcrypto'
实例
import { rsa, ecc, aes } from '@bicycle-codes/crypto-util/webcrypto'
// create some ECC keypairs
const eccKeypair = await ecc.create(KeyUse.Sign)
const eccSignKeys = await ecc.create(KeyUse.Encrypt)
// get the public key as a string
const publicKey = await ecc.exportPublicKey(eccSignKeys.publicKey)
// get the public key as a DID format string
const did = await ecc.publicKeyToDid(eccSignKeys.publicKey)
// transform a DID string to a public key instance
const publicKey = ecc.didToPublicKey(eccDid)
网络crypto AES API
aes.create
创建新的 AES- GCM 密钥 .
function create (opts:{ alg, length } = {
alg: DEFAULT_SYMM_ALGORITHM,
length: DEFAULT_SYMM_LEN
}):Promise<CryptoKey>
aes.create 实例
import { create } from '@bicycle-codes/crypto-util/webcrypto/aes'
const aesKey = await createAes()
aes.encrypt
加密一个字符串 .
async function encrypt (
msg:Msg,
key:SymmKey|string,
opts?:Partial<SymmKeyOpts>
):Promise<string>
import { encrypt } from '@bicycle-codes/crypto-util/webcrypto/aes'
let aesEncryptedText:string
test('encrypt some text with AES', async t => {
aesEncryptedText = await encrypt('hello AES', aesKey)
// returns a string by default
})
aes.decrypt
async function decrypt (
msg:Msg,
key:SymmKey|string,
opts?:Partial<SymmKeyOpts>,
charSize:CharSize = DEFAULT_CHAR_SIZE
):Promise<string>
import { decrypt } from '@bicycle-codes/crypto-util/webcrypto/aes'
const decrypted = await decrypt(aesEncryptedText, aesKey)
// => 'hello AES'
webcrypto RSA API 页面存档备份,存于互联网档案馆
我们曝光RSA,因为并非所有浏览器都支持ECC密钥. 见 ./src/rsa/webcrypto.ts 财务报告和审定财务报表 ./测试/索引.ts。 。 。 .
eCC API 网络催眠
ecc.create
async function create (
use:KeyUse,
curve:EccCurve = EccCurve.P_256,
):Promise<CryptoKeyPair>
create 实例
import { create } from '@bicycle-codes/crypto-util/webcrypto/ecc'
const alicesEncryptionKeys = await createEcc(KeyUse.Encrypt)
const alicesSigningKeys = await createEcc(KeyUse.Sign)
sign
async function sign (
msg:Msg, // <-- string or Uint8Array
privateKey:PrivateKey,
{ format }:{ format: 'base64'|'raw' } = { format: 'base64' },
charSize:CharSize = DEFAULT_CHAR_SIZE,
hashAlg:HashAlg = DEFAULT_HASH_ALGORITHM,
):Promise<ArrayBuffer|string>
实例
import { sign } from '@bicycle-codes/crypto-util/webcrypto/ecc'
const sig = await sign('hello webcrypto', eccSignKeys.privateKey)
verifyWithDid
用 DD 格式字符串验证签名 .
async function verifyWithDid (
msg:string,
sig:string,
did:DID
):Promise<boolean>
import { verifyWithDid } from '@bicycle-codes/crypto-util/webcrypto/ecc'
const isOk = await verifyWithDid('hello dids', sig, did)
getSharedKey
获得两个现有密钥的共享密钥 .
async function getSharedKey (
privateKey:PrivateKey,
publicKey:PublicKey,
opts?:Partial<{
alg:'AES-GCM'|'AES-CBC'|'AES-CTR'
length:SymmKeyLength
iv:ArrayBuffer
}>
):Promise<SymmKey>
let BobsKeys:CryptoKeyPair
let sharedKey:CryptoKey
const BobsKeys = await createEcc(KeyUse.Encrypt)
const sharedKey = await getSharedKey(eccKeypair.privateKey, BobsKeys.publicKey)
t.ok(sharedKey instanceof CryptoKey, 'should return a `CryptoKey`')
encrypt
用你的私人钥匙和收件人的公用钥匙加密一些东西.
async function encrypt (
msg:Msg, // <-- string or Uint8Array
privateKey:PrivateKey,
publicKey:string|PublicKey, // <-- base64 or key
{ format }:{ format: 'base64'|'raw' } = { format: 'base64' },
charSize:CharSize = DEFAULT_CHAR_SIZE,
curve:EccCurve = DEFAULT_ECC_CURVE,
opts?:Partial<{
alg:SymmAlg
length:SymmKeyLength
iv:ArrayBuffer
}>
):Promise<Uint8Array|string>
实例
const eccEncryptedText = await ecc.encrypt(
'hello ecc',
alicesKeys.privateKey,
BobsKeys.publicKey
)
decrypt
解密一些被加密的文本 ecc.ecncrypt。 。 。 .
async function decrypt (
msg:Msg, // <-- string or Uint8Array
privateKey:PrivateKey,
publicKey:string|PublicKey,
curve:EccCurve = DEFAULT_ECC_CURVE,
opts?:Partial<{
alg:'AES-GCM'|'AES-CBC'|'AES-CTR'
length:SymmKeyLength
iv:ArrayBuffer
}>
):Promise<string>
实例
注意这里互换的密钥 -- 公用和私用密钥都可以来自其中之一 keypair 还能用.
const decrypted = await ecc.decrypt(
eccEncryptedText,
BobsKeys.privateKey,
alicesKeys.publicKey
)
aPI 钠
在联署材料可以运行的地方,这些工具应发挥作用.
import { aes, ecc, rsa } from '@bicycle-codes/crypto-util/sodium'
或导入单个模块
import * as aes from '@bicycle-codes/crypto-util/sodium/aes'
import * as ecc from '@bicycle-codes/crypto-util/sodium/ecc'
import * as webcryptoAes from '@bicycle-codes/crypto-util/webcrypto/aes'
AES API钠
加密为 非洲地理信息系统256 (对称密码).
钠 + AES 实例
import {
create,
encrypt,
decrypt
} from '@bicycle-codes/crypto-util/sodium/aes'
// create a new key
const key = await create()
// or create a key, return a Uint8Array
const keyAsBuffer = await createAes({ format: 'raw' })
// encrypt something
const encryptedString = await encrypt('hello sodium + AES', key)
aes.create
创建一个新的 AES 密钥 。 过 { format: 'raw' } 将返回 a Uint8Array。 。 。 .
async function create (opts:{
format: 'string'|'raw'
} = { format: 'string' }):Promise<Uint8Array|string>
实例
import { create } from '@bicycle-codes/crypto-util/sodium/aes'
const aesKey = await create()
aes.encrypt
加密给定的字符串或缓冲器。 过 { format: 'raw' } 将返回 a Uint8Array。 。 。 .
async function encrypt (
msg:Uint8Array|string,
key:Uint8Array|string,
opts:Partial<{
iv?:Uint8Array
format?:'string'|'raw'
}> = { format: 'string' },
):Promise<Uint8Array|string>
实例
import { encrypt } from '@bicycle-codes/crypto-util/sodium/aes'
const encryptedString = await encryptAes('hello sodium + AES', aesKey)
aes.decrypt
解密给定的字符串或缓冲. 过 { format: 'raw' } 将返回 a Uint8Array。 。 。 .
async function decrypt (
cipherText:string|Uint8Array,
key:string|Uint8Array,
opts:{ format:'string'|'raw' } = { format: 'string' }
):Promise<Uint8Array|string>
实例
import { decrypt } from '@bicycle-codes/sodium/aes'
const decrypted = await decrypt(encryptedAes, aesKey)
// => "hello sodium + AES"
ECC API钠
钠+ECC实例
import * as ecc from '@bicycle-codes/crypto-util/sodium/ecc'
const keys = await ecc.create()
ecc.create
创造出一个新的Edward keypair.
async function create (
use:KeyUse,
curve:EccCurve = EccCurve.P_256,
):Promise<CryptoKeyPair>
.create 实例
import { create } from '@bicycle-codes/crypto-util/sodium/ecc'
const keys = await create()
ecc.sign
为下潜数据创建签名 。 过 { format: 'raw' } 获取一个 Uint8Array 而不是一个字符串.
async function sign (
data:string|Uint8Array,
key:LockKey,
opts:{
format:'string'|'raw'
} = { format: 'string' }
):Promise<string|Uint8Array>
ecc.sign 实例
import { sign } from '@bicycle-codes/crypto-util/webcrypto/ecc'
const sig = await sign('hello webcrypto', alicesKeys, { format: 'raw' })
ecc.publicKeyToDid
选择一个公钥实例并返回一个 DED 格式字符串 .
async function publicKeyToDid (
publicKey:Uint8Array|PublicKey
):Promise<DID>
ecc.publicKeyToDid 实例
import {
exportPublicKey,
publicKeyToDid
} from '@bicycle-codes/crypto-util/webcrypto/ecc'
const arr = await exportPublicKey(eccSignKeys.publicKey)
const did = await publicKeyToDid(arr)
ecc.verify
验证给定的签名 + 公钥 + 消息数据 .
async function verify (
msg:Msg, // <-- string or Uint8Array
sig:string|Uint8Array|ArrayBuffer,
publicKey:string|PublicKey,
charSize:CharSize = DEFAULT_CHAR_SIZE,
curve:EccCurve = DEFAULT_ECC_CURVE,
hashAlg: HashAlg = DEFAULT_HASH_ALGORITHM
):Promise<boolean>
ecc.verify 实例
const key = await importDid(eccDid)
const isOk = await verify('hello webcrypto', sig, key)
t.ok(isOk, 'should verify a valid signature')
ecc.verifyWithDid
校验给定的签名 + 消息 + 公用密钥是好的, 使用给定的 DD 字符串作为公用密钥材料 .
async function verifyWithDid (
msg:string,
sig:string,
did:DID
):Promise<boolean>
ecc.verifyWithDid 实例
import { verifyWithDid } from '@bicycle-codes/crypto-util/webcrypto/ecc'
const isOk = await verifyWithDid('hello dids', sig, did)
ecc.encrypt
使用给定的私人和公共密钥创建共享密钥,然后用密钥加密消息.
async function encrypt (
msg:Msg,
privateKey:PrivateKey,
publicKey:string|PublicKey, // <-- base64 or key
{ format }:{ format: 'base64'|'raw' } = { format: 'base64' },
charSize:CharSize = DEFAULT_CHAR_SIZE,
curve:EccCurve = DEFAULT_ECC_CURVE,
opts?:Partial<{
alg:SymmAlg
length:SymmKeyLength
iv:ArrayBuffer
}>
):Promise<Uint8Array|string>
ecc.encrypt example
import { encrypt } from '@bicycle-codes/crypto-util/webcrypto/ecc'
const eccEncryptedText = await encrypt(
'hello ecc',
AlicesKeys.privateKey,
BobsKeys.publicKey
)
ecc.decrypt
解密一个消息给一个公用和私用密钥. 在示例中,键盘被反转 encrypt 实例。 这通过 迪菲·赫尔曼。 。 。 .
import { decrypt } from '@bicycle-codes/crypto-util/webcrypto/ecc'
const decrypted = await decrypt(
eccEncryptedText,
BobsKeys.privateKey,
AlicesKeys.publicKey
)
另见
- [问]AES GCM——IV长度#74 - 出版这一出版物的部分动机
libsodium文档除非您绝对需要 AES- GCM, 请使用 AEGIS-256 (crypto aead aegis256 * ()) 代替 。 它没有任何这些限制.
- 网络加密 API - MDN 文件
- 存储管理器: 持久性方法
- idb 键 -- 超简单的密钥值存储 API 构建于
indexedDB。 。 。 .