JSLib Reference Manual / Exports / SBCrypto
Class: SBCrypto#
SBCrypto
SBCrypto contains all the SB specific crypto functions, as well as some general utility functions.
Table of contents#
Constructors#
Methods#
Constructors#
constructor#
• new SBCrypto()
Methods#
ab2str#
▸ ab2str(buffer): string
Standardized ‘ab2str()’ function, array buffer to string. This assumes one byte per character.
Parameters#
Name |
Type |
|---|---|
|
|
Returns#
string
string
addKnownKey#
▸ addKnownKey(key): Promise<void>
SBCrypto.addKnownKey()
Adds any key to the list of known keys; if it’s known but only as a public key, then it will be ‘upgraded’.
Parameters#
Name |
Type |
|---|---|
|
|
Returns#
Promise<void>
channelKeyStringsToCryptoKeys#
▸ channelKeyStringsToCryptoKeys(keyStrings): Promise<ChannelKeys>
Parameters#
Name |
Type |
|---|---|
|
Returns#
Promise<ChannelKeys>
compareHashWithKey#
▸ compareHashWithKey(hash, key): Promise<boolean>
SBCrypto.compareHashWithKey()
Checks if an existing SB384Hash is ‘compatible’ with a given key.
Note that you CAN NOT have a hash, and a key, generate a hash from that key, and then compare the two. The hash generation per se will be deterministic and specific AT ANY POINT IN TIME, but may change over time, and this comparison function will maintain ability to compare over versions.
For example, this comparison will accept a simple straight b64-encoded hash without iteration or other processing.
Parameters#
Name |
Type |
|---|---|
|
|
|
|
Returns#
Promise<boolean>
compareKeys#
▸ compareKeys(key1, key2): boolean
SBCrypto.compareKeys()
Compare JSON keys, true if the ‘same’, false if different. We consider them “equal” if both have ‘x’ and ‘y’ properties and they are the same. (Which means it doesn’t care about which or either being public or private)
Parameters#
Name |
Type |
|---|---|
|
|
|
|
Returns#
boolean
deriveKey#
▸ deriveKey(privateKey, publicKey, type, extractable, keyUsages): Promise<CryptoKey>
SBCrypto.deriveKey()
Derive key. Takes a private and public key, and returns a Promise to a cryptoKey for 1:1 communication.
Parameters#
Name |
Type |
|---|---|
|
|
|
|
|
|
|
|
|
|
Returns#
Promise<CryptoKey>
encrypt#
▸ encrypt(data, key, _iv?, returnType?): Promise<EncryptedContents>
SBCrypto.encrypt()
Encrypt. if no nonce (iv) is given, will create it. Returns a Promise that resolves either to raw array buffer or a packaged EncryptedContents. Note that for the former, nonce must be given.
Parameters#
Name |
Type |
|---|---|
|
|
|
|
|
|
|
|
Returns#
Promise<EncryptedContents>
▸ encrypt(data, key, _iv?, returnType?): Promise<ArrayBuffer>
Parameters#
Name |
Type |
|---|---|
|
|
|
|
|
|
|
|
Returns#
Promise<ArrayBuffer>
exportKey#
▸ exportKey(format, key): Promise<undefined | JsonWebKey>
SBCrypto.exportKey()
Export key; note that if there’s an issue, this will return undefined. That can happen normally if for example the key is restricted (and not extractable).
Parameters#
Name |
Type |
|---|---|
|
|
|
|
Returns#
Promise<undefined | JsonWebKey>
extractPubKey#
▸ extractPubKey(privateKey): null | JsonWebKey
Extracts (generates) public key from a private key.
Parameters#
Name |
Type |
|---|---|
|
|
Returns#
null | JsonWebKey
generateIdKey#
▸ generateIdKey(buf): Promise<{ id: string ; key: string }>
Hashes and splits into two (h1 and h1) signature of data, h1 is used to request (salt, iv) pair and then h2 is used for encryption (h2, salt, iv)
Parameters#
Name |
Type |
Description |
|---|---|---|
|
|
blob of data to be stored |
Returns#
Promise<{ id: string ; key: string }>
generateKeys#
▸ generateKeys(): Promise<CryptoKeyPair>
SBCrypto.generatekeys()
Generates standard ECDH keys using P-384.
Returns#
Promise<CryptoKeyPair>
importKey#
▸ importKey(format, key, type, extractable, keyUsages): Promise<CryptoKey>
SBCrypto.importKey()
Import keys
Parameters#
Name |
Type |
|---|---|
|
|
|
|
|
|
|
|
|
|
Returns#
Promise<CryptoKey>
lookupKey#
▸ lookupKey(key, array): number
SBCrypto.lookupKey()
Uses compareKeys() to check for presense of a key in a list of keys. Returns index of key if found, -1 if not found.
Parameters#
Name |
Type |
|---|---|
|
|
|
|
Returns#
number
lookupKeyGlobal#
▸ lookupKeyGlobal(hash): undefined | knownKeysInfo
SBCrypto.lookupKeyGlobal()
Given any sort of SB384Hash, returns the corresponding known key, if any
Parameters#
Name |
Type |
|---|---|
|
|
Returns#
undefined | knownKeysInfo
sb384Hash#
▸ sb384Hash(key?): Promise<undefined | string>
SBCrypto.sb384Hash()
Takes a JsonWebKey and creates the SB384Hash. Returns
Parameters#
Name |
Type |
|---|---|
|
|
Returns#
Promise<undefined | string>
sign#
▸ sign(secretKey, contents): Promise<string>
SBCrypto.sign()
Sign
Parameters#
Name |
Type |
|---|---|
|
|
|
|
Returns#
Promise<string>
str2ab#
▸ str2ab(string): Uint8Array
Standardized ‘str2ab()’ function, string to array buffer. This assumes on byte per character.
Parameters#
Name |
Type |
|---|---|
|
|
Returns#
Uint8Array
buffer
unwrap#
▸ unwrap(k, o, returnType): Promise<string>
SBCrypto.unwrap
Decrypts a wrapped object, returns (promise to) decrypted contents per se (either as a string or arrayBuffer)
Parameters#
Name |
Type |
|---|---|
|
|
|
|
|
|
Returns#
Promise<string>
▸ unwrap(k, o, returnType): Promise<ArrayBuffer>
Parameters#
Name |
Type |
|---|---|
|
|
|
|
|
|
Returns#
Promise<ArrayBuffer>
verify#
▸ verify(verifyKey, sign, contents): Promise<boolean>
SBCrypto.verify()
Verify signature.
Parameters#
Name |
Type |
|---|---|
|
|
|
|
|
|
Returns#
Promise<boolean>
verifyChannelId#
▸ verifyChannelId(owner_key, channel_id): Promise<boolean>
‘Compare’ two channel IDs. Note that this is not constant time.
Parameters#
Name |
Type |
|---|---|
|
|
|
|
Returns#
Promise<boolean>
wrap#
▸ wrap(k, b, bodyType): Promise<EncryptedContents>
Parameters#
Name |
Type |
|---|---|
|
|
|
|
|
|
Returns#
Promise<EncryptedContents>
▸ wrap(k, b, bodyType): Promise<EncryptedContents>
Parameters#
Name |
Type |
|---|---|
|
|
|
|
|
|
Returns#
Promise<EncryptedContents>