Super-Simple Crypto is a wrapper around other cryptography libraries, intended to be simple to use, provide a consistent interface for multiple encryption backends (for now, forge and nodeJS crypto), and well-chosen parameters.
It was created by Seald to unify crypto accross its projects.
API:
-
-
Properties
-
Methods
-
-
Properties
-
Methods
-
-
Properties
-
Methods
npm i -S sscrypto// ES Module syntax
import { node } from 'sscrypto' // this may cause trouble if you do not have forge installed and are not using a build-system with tree-shaking
// or
import { SymKey, PrivateKey, PublicKey } from 'sscrypto/node'
// or
import SymKey from 'sscrypto/node/aes'
import { PrivateKey, PublicKey } from 'sscrypto/node/rsa'
// CommonJS syntax
const { node } = require('sscrypto') // this may cause trouble if you do not have forge installed and are not using a build-system with tree-shaking
// or
const { SymKey, PrivateKey, PublicKey } = require('sscrypto/node')
// or
const SymKey = require('sscrypto/node/aes')
const { PrivateKey, PublicKey } = require('sscrypto/node/rsa')npm i -S sscrypto node-forge// ES Module syntax
import { forge } from 'sscrypto'
// or
import { SymKey, PrivateKey, PublicKey, utils } from 'sscrypto/forge'
// or
import SymKey from 'sscrypto/forge/aes'
import { PrivateKey, PublicKey } from 'sscrypto/forge/rsa'
// CommonJS syntax
const { node } = require('sscrypto')
// or
const { SymKey, PrivateKey, PublicKey, utils } = require('sscrypto/node')
// or
const SymKey = require('sscrypto/node/aes')
const { PrivateKey, PublicKey } = require('sscrypto/node/rsa')⊕ new SymKey(arg?: SymKeySize | Buffer): SymKey
Constructor of SymKey, if you want to construct an SymKey with an existing key, use the static methods SymKey.fromString or fromB64 Defaults to a new 256 bits key.
constructs: SymKey
Parameters:
| Name | Type | Default value |
|---|---|---|
Default value arg |
SymKeySize | Buffer |
256 |
Returns: SymKey
● encryptionKey: string
● keySize: number
● signingKey: string
▸ calculateHMAC(textToAuthenticate: Buffer): Buffer
Calculates a SHA-256 HMAC with the SymKey#signingKey on the textToAuthenticate
Parameters:
| Name | Type | Description |
|---|---|---|
| textToAuthenticate | Buffer |
- |
Returns: Buffer
▸ decrypt(cipheredMessage: Buffer): Buffer
Decrypts the cipheredMessage using the same algorithms as SymKey#encrypt
Parameters:
| Name | Type | Description |
|---|---|---|
| cipheredMessage | Buffer |
- |
Returns: Buffer
▸ decryptStream(): Transform
Creates a Transform stream that decrypts the encrypted data piped to it.
Returns: Transform
▸ encrypt(clearText: Buffer): Buffer
Encrypts the clearText with SymKey#encryptionKey using AES-CBC, and a SHA-256 HMAC calculated with SymKey#signingKey, returns it concatenated in the following order: InitializationVector CipherText HMAC
Parameters:
| Name | Type | Description |
|---|---|---|
| clearText | Buffer |
- |
Returns: Buffer
▸ encryptStream(): Transform
Creates a Transform stream that encrypts the data piped to it.
Returns: Transform
▸ toB64(): string
Returns both SymKey#signingKey and SymKey#encryptionKey concatenated encoded with b64
Returns: string
▸ toString(): string
Returns both SymKey#signingKey and SymKey#encryptionKey concatenated as a binary string
Returns: string
▸ fromB64(messageKey: string): SymKey
Static method to construct a new SymKey from a b64 encoded messageKey
Parameters:
| Name | Type | Description |
|---|---|---|
| messageKey | string |
b64 encoded messageKey |
Returns: SymKey
▸ fromString(messageKey: string): SymKey
Static method to construct a new SymKey from a binary string encoded messageKey
Parameters:
| Name | Type | Description |
|---|---|---|
| messageKey | string |
binary encoded messageKey |
Returns: SymKey
PublicKey
⊕ new PublicKey(key: Buffer): PublicKey
PublicKey constructor. Should be given a Buffer containing a DER serialization of the key.
constructs: PublicKey
Parameters:
| Name | Type | Description |
|---|---|---|
| key | Buffer |
Returns: PublicKey
● publicKey: PublicKey
▸ encrypt(clearText: Buffer, doCRC?: boolean): Buffer
Encrypts a clearText for the Private Key corresponding to this PublicKey.
method:
Parameters:
| Name | Type | Default value | Description |
|---|---|---|---|
| clearText | Buffer |
- | - |
Default value doCRC |
boolean |
true | - |
Returns: Buffer
▸ getB64Hash(): string
Returns: string
▸ getHash(): string
Returns: string
▸ toB64(options?: __type): string
Serializes the key to DER format and encodes it in b64.
method:
Parameters:
| Name | Type | Default value |
|---|---|---|
Default value options |
__type |
null |
Returns: string
▸ verify(textToCheckAgainst: Buffer, signature: Buffer): boolean
Verify that the message has been signed with the Private Key corresponding to this PublicKey.
Parameters:
| Name | Type | Description |
|---|---|---|
| textToCheckAgainst | Buffer |
- |
| signature | Buffer |
- |
Returns: boolean
▸ fromB64(b64DERFormattedPublicKey: string): PublicKey
Returns a PublicKey from it's DER base64 serialization.
method:
static:
Parameters:
| Name | Type | Description |
|---|---|---|
| b64DERFormattedPublicKey | string |
a b64 encoded public key formatted with DER |
Returns: PublicKey
↳ PrivateKey
⊕ new PrivateKey(key: Buffer): PrivateKey
Overrides PublicKey.constructor
Private Key constructor. Shouldn't be used directly, use fromB64 or generate static methods
constructs: PrivateKey
Parameters:
| Name | Type | Description |
|---|---|---|
| key | Buffer |
Returns: PrivateKey
● privateKey: PrivateKey
● publicKey: PublicKey
Inherited from PublicKey.publicKey
▸ decrypt(cipherText: Buffer, doCRC?: boolean): Buffer
Deciphers the given message.
Parameters:
| Name | Type | Default value | Description |
|---|---|---|---|
| cipherText | Buffer |
- | - |
Default value doCRC |
boolean |
true |
Returns: Buffer
▸ encrypt(clearText: Buffer, doCRC?: boolean): Buffer
Inherited from PublicKey.encrypt
Encrypts a clearText for the Private Key corresponding to this PublicKey.
method:
Parameters:
| Name | Type | Default value | Description |
|---|---|---|---|
| clearText | Buffer |
- | - |
Default value doCRC |
boolean |
true | - |
Returns: Buffer
▸ getB64Hash(): string
Inherited from PublicKey.getB64Hash
Returns: string
▸ getHash(): string
Inherited from PublicKey.getHash
Returns: string
▸ sign(textToSign: Buffer): Buffer
Signs the given message with this Private Key.
Parameters:
| Name | Type | Description |
|---|---|---|
| textToSign | Buffer |
- |
Returns: Buffer
▸ toB64(__namedParameters?: object): string
Serializes the key to DER format and encodes it in b64.
method:
Parameters:
Default value __namedParameters: object
| Name | Type | Default value |
|---|---|---|
| publicOnly | boolean |
false |
Returns: string
▸ verify(textToCheckAgainst: Buffer, signature: Buffer): boolean
Inherited from PublicKey.verify
Verify that the message has been signed with the Private Key corresponding to this PublicKey.
Parameters:
| Name | Type | Description |
|---|---|---|
| textToCheckAgainst | Buffer |
- |
| signature | Buffer |
- |
Returns: boolean
▸ fromB64(b64DERFormattedPrivateKey: string): PrivateKey
Returns a PrivateKey from it's DER base64 serialization.
method:
static:
Parameters:
| Name | Type | Description |
|---|---|---|
| b64DERFormattedPrivateKey | string |
a b64 encoded private key formatted with DER |
Returns: PrivateKey
▸ generate(size?: AsymKeySize): Promise<PrivateKey>
Generates a PrivateKey asynchronously
Parameters:
| Name | Type | Default value |
|---|---|---|
Default value size |
AsymKeySize | 4096 |
Returns: Promise<PrivateKey>
▸ randomBytes(length?: number): Buffer
Returns a Buffer of random bytes
Parameters:
| Name | Type | Default value |
|---|---|---|
Default value length |
number |
10 |
Returns: Buffer
▸ sha256(data: Buffer): Buffer
Returns a Buffer containing the hash of the given data
Parameters:
| Name | Type | Description |
|---|---|---|
| data | Buffer |
- |
Returns: Buffer
Ƭ SymKeySize: 128 | 192 | 256
Ƭ AsymKeySize: 4096 | 2048 | 1024