New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@consento/crypto

Package Overview
Dependencies
Maintainers
3
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@consento/crypto - npm Package Compare versions

Comparing version 0.5.3 to 0.5.4

src/blob/index.d.ts

2

package.json
{
"name": "@consento/crypto",
"version": "0.5.3",
"version": "0.5.4",
"description": "Crypto functionality used in Consento",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -50,4 +50,4 @@ # @consento/crypto

writer.senderKey // To backup/restore the sender
reader.channelKeyBase64 === writer.channelKeyBase64 === verifier.channelKeyBase64
// The lookup id is same here, the channelKey may be used to verify the data, can also be used for the channel
reader.verifyKeyBase64 === writer.verifyKeyBase64 === verifier.verifyKeyBase64
// The lookup id is same here, the verifyKey may be used to verify the data, can also be used for the channel

@@ -64,4 +64,6 @@ writer.signKey // Allows the writer to sign messages, only privy to the writer

const { createChannel, Reader, Writer, Verifier } = require('@consento/crypto')
const { reader, writer, verifier } = createChannel()
const channel = createChannel()
const { reader, writer, verifier } = channel
new Channel(channel.toJSON())
new Reader(reader.toJSON())

@@ -95,3 +97,3 @@ new Writer(writer.toJSON())

#### writer.encryptOnly(body)
#### writer.encryptOnly(body), reader.encryptOnly(body)

@@ -98,0 +100,0 @@ Only encrypt the body. This is only recommended in an environment where the

@@ -9,6 +9,7 @@ import {

} from '../types'
import { bufferEquals, Buffer } from '../util'
import { Buffer, bufferToString, toBuffer } from '../util'
import { Reader } from './Reader'
import { Writer } from './Writer'
import * as sodium from 'sodium-universal'
import { readerKeyFromChannelKey, writerKeyFromChannelKey } from './key'

@@ -51,24 +52,59 @@ const {

const sign = createSignKeys()
return new Channel({
reader: { readerKey: Buffer.concat([encrypt.publicKey, sign.publicKey, encrypt.privateKey]) },
writer: { writerKey: Buffer.concat([encrypt.publicKey, sign.publicKey, sign.privateKey]) }
})
return new Channel({ channelKey: Buffer.concat([encrypt.publicKey, sign.publicKey, encrypt.privateKey, sign.privateKey]) })
}
export class Channel implements IChannel {
reader: IReader
writer: IWriter
type: 'channel' = 'channel'
_reader?: IReader
_writer?: IWriter
_channelKey?: Uint8Array
_channelKeyBase64?: string
constructor (opts: IChannelOptions) {
this.reader = (opts.reader instanceof Reader) ? opts.reader : new Reader(opts.reader)
this.writer = (opts.writer instanceof Writer) ? opts.writer : new Writer(opts.writer)
if (opts.type !== undefined && opts.type as string !== 'channel') {
throw new Error(`Can not restore a channel from a [${opts.type}]`)
constructor ({ channelKey }: IChannelOptions) {
if (typeof channelKey === 'string') {
this._channelKeyBase64 = channelKey
} else {
this._channelKey = channelKey
}
if (!bufferEquals(this.reader.channelKey, this.writer.channelKey)) {
throw new Error('Can not create a channel with both the writer and the reader have a different id! Did you mean to restore a connection?')
}
get verifyKey (): Uint8Array {
return this.reader.verifyKey
}
get verifyKeyBase64 (): string {
return this.reader.verifyKeyBase64
}
get verifyKeyHex (): string {
return this.reader.verifyKeyHex
}
get channelKey (): Uint8Array {
if (this._channelKey === undefined) {
this._channelKey = toBuffer(this._channelKeyBase64 as unknown as string)
}
return this._channelKey
}
get channelKeyBase64 (): string {
if (this._channelKeyBase64 === undefined) {
this._channelKeyBase64 = bufferToString(this._channelKey as unknown as Uint8Array, 'base64')
}
return this._channelKeyBase64
}
get reader (): IReader {
if (this._reader === undefined) {
this._reader = new Reader({ readerKey: readerKeyFromChannelKey(this.channelKey) })
}
return this._reader
}
get writer (): IWriter {
if (this._writer === undefined) {
this._writer = new Writer({ writerKey: writerKeyFromChannelKey(this.channelKey) })
}
return this._writer
}
get verifier (): IVerifier {

@@ -78,9 +114,11 @@ return this.reader.verifier

toString (): string {
return `Channel[${this.verifyKeyBase64}]`
}
toJSON (): IChannelJSON {
return {
reader: this.reader.toJSON(),
writer: this.writer.toJSON(),
type: 'channel'
channelKey: this.channelKeyBase64
}
}
}

@@ -14,6 +14,3 @@ import { IConnection, IReader, IWriter, IConnectionJSON, IConnectionOptions } from '../types'

this.writer = (opts.writer instanceof Writer) ? opts.writer : new Writer(opts.writer)
if (opts.type !== undefined && opts.type as string !== 'connection') {
throw new Error(`Can not restore a connection from a [${opts.type}]`)
}
if (bufferEquals(this.reader.channelKey, this.writer.channelKey)) {
if (bufferEquals(this.reader.verifyKey, this.writer.verifyKey)) {
throw new Error('Can not create a connection with both the writer and the reader have the same id! Did you mean to restore a channel?')

@@ -26,6 +23,5 @@ }

reader: this.reader.toJSON(),
writer: this.writer.toJSON(),
type: 'connection'
writer: this.writer.toJSON()
}
}
}

@@ -0,1 +1,3 @@

import { Buffer } from '../util'
// Structure of keys:

@@ -5,2 +7,4 @@ //

// SEND_KEY = [ENCRYPT_KEY][VERIFY_KEY][SIGN_KEY]
// CHANNEL_KEY = [ENCRYPT_KEY][VERIFY_KEY][DECRYPT_KEY][SIGN_KEY]
//

@@ -23,2 +27,5 @@ const ENCRYPT_KEY_SIZE = 32

const SIGN_KEY_CHANNEL_START = DECRYPT_KEY_END
const SIGN_KEY_CHANNEL_END = SIGN_KEY_CHANNEL_START + SIGN_KEY_SIZE
export function encryptKeyFromSendOrReceiveKey (sendOrReceiveKey: Uint8Array): Uint8Array {

@@ -39,1 +46,13 @@ return sendOrReceiveKey.slice(ENCRYPT_KEY_START, ENCRYPT_KEY_END)

}
export function signKeyFromChannelKey (channelKey: Uint8Array): Uint8Array {
return channelKey.slice(SIGN_KEY_CHANNEL_START, SIGN_KEY_CHANNEL_END)
}
export function readerKeyFromChannelKey (channelKey: Uint8Array): Uint8Array {
return channelKey.slice(ENCRYPT_KEY_START, DECRYPT_KEY_END)
}
export function writerKeyFromChannelKey (channelKey: Uint8Array): Uint8Array {
return Buffer.concat([channelKey.slice(ENCRYPT_KEY_START, VERIFY_KEY_END), channelKey.slice(SIGN_KEY_CHANNEL_START, SIGN_KEY_CHANNEL_END)])
}
import { IVerifier, IReader, IReaderJSON, IEncryptedMessage, IReaderOptions, IDecryption, EDecryptionError } from '../types'
import { Verifier } from './Verifier'
import { encryptKeyFromSendOrReceiveKey, decryptKeyFromReceiveKey, verifyKeyFromSendOrReceiveKey } from './key'
import { bufferToAny, bufferToString, toBuffer } from '../util'
import { bufferToAny, bufferToString, IEncodable, toBuffer } from '../util'
import { encryptMessage } from '../util/encryptMessage'
import * as sodium from 'sodium-universal'

@@ -43,3 +44,3 @@

_encryptKey?: Uint8Array
_annonymous?: IVerifier
_verifier?: IVerifier

@@ -54,12 +55,12 @@ constructor ({ readerKey: receiveKey }: IReaderOptions) {

get channelKey (): Uint8Array {
return this.verifier.channelKey
get verifyKey (): Uint8Array {
return this.verifier.verifyKey
}
get channelKeyHex (): string {
return this.verifier.channelKeyHex
get verifyKeyHex (): string {
return this.verifier.verifyKeyHex
}
get channelKeyBase64 (): string {
return this.verifier.channelKeyBase64
get verifyKeyBase64 (): string {
return this.verifier.verifyKeyBase64
}

@@ -75,6 +76,6 @@

get verifier (): IVerifier {
if (this._annonymous === undefined) {
this._annonymous = new Verifier({ channelKey: verifyKeyFromSendOrReceiveKey(this.readerKey) })
if (this._verifier === undefined) {
this._verifier = new Verifier({ verifyKey: verifyKeyFromSendOrReceiveKey(this.readerKey) })
}
return this._annonymous
return this._verifier
}

@@ -108,8 +109,12 @@

toString (): string {
return `Reader[${this.channelKeyBase64}]`
return `Reader[${this.verifyKeyBase64}]`
}
encryptOnly (message: IEncodable): Uint8Array {
return encryptMessage(this.encryptKey, message)
}
decrypt (encrypted: IEncryptedMessage): IDecryption {
return decryptMessage(
this.verifier.channelKey,
this.verifier.verifyKey,
this.encryptKey,

@@ -116,0 +121,0 @@ this.decryptKey,

@@ -8,33 +8,33 @@ import { bufferToString, toBuffer } from '../util'

export class Verifier implements IVerifier {
_id?: Uint8Array
_idBase64?: string
_idHex?: string
_verifyKey?: Uint8Array
_verifyKeyBase64?: string
_verifyKeyHex?: string
constructor ({ channelKey: id }: IVerifierOptions) {
constructor ({ verifyKey: id }: IVerifierOptions) {
if (typeof id === 'string') {
this._idBase64 = id
this._verifyKeyBase64 = id
} else {
this._id = id
this._verifyKey = id
}
}
get channelKey (): Uint8Array {
if (this._id === undefined) {
this._id = toBuffer(this._idBase64 as unknown as string)
get verifyKey (): Uint8Array {
if (this._verifyKey === undefined) {
this._verifyKey = toBuffer(this._verifyKeyBase64 as unknown as string)
}
return this._id
return this._verifyKey
}
get channelKeyBase64 (): string {
if (this._idBase64 === undefined) {
this._idBase64 = bufferToString(this._id as unknown as Uint8Array, 'base64')
get verifyKeyBase64 (): string {
if (this._verifyKeyBase64 === undefined) {
this._verifyKeyBase64 = bufferToString(this._verifyKey as unknown as Uint8Array, 'base64')
}
return this._idBase64
return this._verifyKeyBase64
}
get channelKeyHex (): string {
if (this._idHex === undefined) {
this._idHex = bufferToString(this.channelKey, 'hex')
get verifyKeyHex (): string {
if (this._verifyKeyHex === undefined) {
this._verifyKeyHex = bufferToString(this.verifyKey, 'hex')
}
return this._idHex
return this._verifyKeyHex
}

@@ -44,3 +44,3 @@

return {
channelKey: this.channelKeyBase64
verifyKey: this.verifyKeyBase64
}

@@ -50,12 +50,12 @@ }

toString (): string {
return `Verifier[${this.channelKeyBase64}]`
return `Verifier[${this.verifyKeyBase64}]`
}
verify (signature: Uint8Array, body: Uint8Array): boolean {
return verify(signature, body, this.channelKey)
return verify(signature, body, this.verifyKey)
}
verifyMessage (message: IEncryptedMessage): boolean {
return verify(message.signature, message.body, this.channelKey)
return verify(message.signature, message.body, this.verifyKey)
}
}
import { IVerifier, IWriter, IWriterJSON, IEncryptedMessage, IWriterOptions } from '../types'
import { Verifier } from './Verifier'
import { encryptKeyFromSendOrReceiveKey, signKeyFromSendKey, verifyKeyFromSendOrReceiveKey } from './key'
import { anyToBuffer, bufferToString, toBuffer, IEncodable } from '../util'
import { bufferToString, toBuffer, IEncodable } from '../util'
import { encryptMessage } from '../util/encryptMessage'
import * as sodium from 'sodium-universal'
const {
crypto_box_SEALBYTES: CRYPTO_BOX_SEALBYTES,
crypto_box_seal: boxSeal,
crypto_sign_BYTES: CRYPTO_SIGN_BYTES,

@@ -21,9 +20,2 @@ crypto_sign_detached: signDetached,

function encryptMessage (writeKey: Uint8Array, message: IEncodable): Uint8Array {
const msgBuffer = anyToBuffer(message)
const body = malloc(msgBuffer.length + CRYPTO_BOX_SEALBYTES)
boxSeal(body, msgBuffer, writeKey)
return body
}
export class Writer implements IWriter {

@@ -72,12 +64,12 @@ _sendKey?: Uint8Array

get channelKey (): Uint8Array {
return this.verifier.channelKey
get verifyKey (): Uint8Array {
return this.verifier.verifyKey
}
get channelKeyHex (): string {
return this.verifier.channelKeyHex
get verifyKeyHex (): string {
return this.verifier.verifyKeyHex
}
get channelKeyBase64 (): string {
return this.verifier.channelKeyBase64
get verifyKeyBase64 (): string {
return this.verifier.verifyKeyBase64
}

@@ -87,3 +79,3 @@

if (this._annonymous === undefined) {
this._annonymous = new Verifier({ channelKey: verifyKeyFromSendOrReceiveKey(this.writerKey) })
this._annonymous = new Verifier({ verifyKey: verifyKeyFromSendOrReceiveKey(this.writerKey) })
}

@@ -98,3 +90,3 @@ return this._annonymous

toString (): string {
return `Writer[${this.channelKeyBase64}]`
return `Writer[${this.verifyKeyBase64}]`
}

@@ -101,0 +93,0 @@

@@ -25,13 +25,13 @@ /* eslint-disable @typescript-eslint/method-signature-style */

export interface IVerifierJSON {
channelKey: string
verifyKey: string
}
export interface IVerifierOptions {
channelKey: IStringOrBuffer
verifyKey: IStringOrBuffer
}
export interface IChannelActor {
readonly channelKey: Uint8Array
readonly channelKeyBase64: string
readonly channelKeyHex: string
readonly verifyKey: Uint8Array
readonly verifyKeyBase64: string
readonly verifyKeyHex: string
}

@@ -84,33 +84,34 @@

decrypt(encrypted: IEncryptedMessage | Uint8Array): IDecryption
encryptOnly(message: IEncodable): Uint8Array
}
export type ComType = 'channel' | 'connection'
export interface IComJSON<TType = ComType> {
export interface IConnectionJSON {
reader: IReaderJSON
writer: IWriterJSON
type: TType
}
export interface IComOptions<TType = ComType> {
export interface IConnectionOptions {
writer: IWriterOptions
reader: IReaderOptions
type?: TType
}
export interface ICom<TType = ComType> {
writer: IWriter
reader: IReader
type: TType
toJSON(): IComJSON<TType>
export interface IChannelJSON {
channelKey: string
}
export type IConnection = ICom<'connection'>
export type IConnectionJSON = IComJSON<'connection'>
export type IConnectionOptions = IComOptions<'connection'>
export interface IChannel extends ICom<'channel'> {
export interface IChannelOptions {
channelKey: IStringOrBuffer
}
export interface IChannel extends IChannelActor {
verifier: IVerifier
toJSON(): IChannelJSON
}
export type IChannelJSON = IComJSON<'channel'>
export type IChannelOptions = IComOptions<'channel'>
export interface IConnection {
writer: IWriter
reader: IReader
toJSON(): IConnectionJSON
}
export interface IHandshakeInitJSON {

@@ -117,0 +118,0 @@ receiver: IReaderJSON

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc