miniLockLib is a
little miniLock library
for ECMAScript.
It supplies you with a set of functions
to perform miniLock encryption operations
in a web agent window or a Node.js environment.
Generates a key pair from a secretPhrase and emailAddress.
Returns a KeyPairOperation.
Your callback should be prepared to receive a set of keys or an error.
secretPhrase = document.querySelector("#secret_phrase_input").value
emailAddress = document.querySelector("#email_address_input").value
miniLockLib.makeKeyPair(secretPhrase, emailAddress, function(error, keys){
if (keys) {
keys.publicKey is a Uint8Array
keys.secretKey is a Uint8Array
} else {
error is a String
}
})
secretPhrase and emailAddress are both tested to ensure they meet miniLock’s input requirements.
If either input is unacceptable your callback will receive one of the following error strings:
"Can’t make keys without a secret phrase."
"Can’t make keys because secretPhrase is not an acceptable secret phrase."
"Can’t make keys without an email address."
"Can’t make keys because emailAddress is not an acceptable email address."
If all the input is OK your callback will receive a set of keys when the operation is complete.
It typically takes two-to-three seconds to complete a key pair operation.
miniLockLib.encrypt({
data: new Blob(["Hello World"]),
name: 'Secret Greeting.txt'
keys: {publicKey: Uint8Array, secretKey: Uint8Array},
miniLockIDs: [aliceID, bobbyID, ...]
callback: function(error, encrypted) {
if (encrypted) {
encrypted.data is a Blob
encrypted.data.name is 'Secret Greeting.txt.minilock'
encrypted.data.type is 'application/minilock'
encrypted.senderID identifies the owner of the encryption keys
} else {
error is a String
}
}
})
miniLockLib.encrypt accepts the following parameters:
data is a Blob to be encrypted.
An instance of File is also acceptable.
name specifies the filename as a String.
keys defines the key pair that will be used to encrypt this file.
miniLockIDs defines who is permitted to decrypt this file.
callback should be prepared to receive encrypted.data or an error.
Potential error strings are as follows:
"Can’t encrypt without a Blob of data."
"Can’t encrypt without a set of keys."
"Can’t encrypt without an Array of miniLock IDs."
"Can’t encrypt because file name is too long. 256-characters max please."
"Can’t encrypt because media type is too long. 128-characters max please."
"Can’t encrypt because version X is not supported. Version 1 or 2 please."
"Failed to encrypt slice of data at [startOfSlice..endOfSlice]"
miniLockLib.decrypt({
data: encrypted.data,
keys: {publicKey: Uint8Array, secretKey: Uint8Array},
callback: function(error, decrypted) {
if (decrypted) {
decrypted.data is a Blob
decrypted.name is the filename as a String
decrypted.senderID identifies the author of the encrypted file
} else {
error is a String
}
}
})
miniLockLib.decrypt accepts the following parameters:
data is a Blob to be decrypted.
An instance of File is also acceptable.
keys defines the key pair that will be used to unlock the file.
callback is a function that will receive decrypted.data or an error.
Potential error strings are as follows:
"Can’t decrypt without a Blob of data."
"Can’t decrypt without a set of keys."
"Can’t decrypt this file with this set of keys."
"Failed to decrypt version 1 file attributes."
"Failed to decrypt version 2 file attributes."
"Failed to decrypt slice of data at [startOfSlice..endOfSlice]"
SecretPhrase
The secret phrase module supplies an acceptance test to validate secret phrase input.
Validates a secret phrase input String.
Returns true or false.
The secretPhrase must be at least 32 characters long and it must contain at least 100 bits of entropy.
EmailAddress
The email address module supplies an acceptance test to validate email address input.
Defines the key pair that will be used to lock this file.
EncryptOperation::start
encryptOperation.start(callback)
Start the operation and define a callback that will receive encrypted.data when the operation is complete or an error when there is a failure.
encryptOperation.start(function(error, decrypted){
if (decrypted) {
encrypted.data is a Blob
encrypted.name is the filename as a String
encrypted.senderID identifies the author of the encrypted file.
} else {
console.error(error)
}
})
If the operation fails your callback will receive one of the following error strings:
"Can’t encrypt without a Blob of data."
"Can’t encrypt without a set of keys."
"Can’t encrypt without an Array of miniLock IDs."
"Can’t encrypt because file name is too long. 256-characters max please."
"Can’t encrypt because media type is too long. 128-characters max please."
"Can’t encrypt because version X is not supported. Version 1 or 2 please."
"Failed to encrypt slice of data at [startOfSlice..endOfSlice]"
DecryptOperation
new miniLockLib.DecryptOperation(params)
Decrypt operations unlock miniLock files.
The constructor accepts data and keys parameters or
you can define them on the operation before you call start.
decryptOperation = new miniLockLib.DecryptOperation({
data: new Blob
keys: {publicKey:, secretKey:}
})
DecryptOperation::data
decryptOperation.data = blob
Defines the data that should be decrypted.
DecryptOperation::keys
decryptOperation.keys = keys
Defines the public–secret key pair that should be used to unlock this file.
DecryptOperation::start
decryptOperation.start(callback)
Start the operation and define a callback that will receive decrypted.data
when the operation is complete or an error when there is a failure.
decryptOperation.start(function(error, decrypted){
if (decrypted) {
decrypted.data is a Blob
decrypted.name is the filename as a String
decrypted.senderID identifies the author of the encrypted file.
} else {
console.error(error)
}
})
If the operation fails your callback will receive one of the following error strings:
"Can’t decrypt without a Blob of data."
"Can’t decrypt without a set of keys."
"Can’t decrypt this file with this set of keys."
"Failed to decrypt version 1 file attributes."
"Failed to decrypt version 2 file attributes."
"Failed to decrypt slice of data at [startOfSlice..endOfSlice]"
KeyPairOperation
new miniLockLib.KeyPairOperation(params)
Key pair operations generate a set of keys from a secret phrase and email address.
The constructor accepts secretPhrase and emailAddress input parameters.
keyPairOperation = new miniLockLib.KeyPairOperation({
secretPhrase: "lions and tigers are not the only ones i am worried about"
emailAddress: "alice@example.com"
})
Or you may define them on the instance before calling start.
keyPairOperation = new miniLockLib.KeyPairOperation
keyPairOperation.secretPhrase = "lions and tigers are not the only ones i am worried about"
keyPairOperation.emailAddress = "alice@example.com"
keyPairOperation.start(...)
KeyPairOperation::secretPhrase
keyPairOperation.secretPhrase = secretPhrase
Should be a String.
Becomes the secret that is used to derive a set of keys.
The phrase must be at least 32 characters long and it must contain at least 100 bits of entropy.
KeyPairOperation::emailAddress
keyPairOperation.emailAddress = emailAddress
Should be a String.
Becomes the salt that is used to derive a set of keys.
KeyPairOperation::start
keyPairOperation.start(callback)
Start the operation and define a callback that will receive a set of keys when the operation is complete or an error when there is a failure.
keyPairOperation.start(function(error, keys){
if (keys) {
keys.publicKey is a Uint8Array
keys.secretKey is a Uint8Array
} else {
error is a String
}
})
The secretPhrase and emailAddress inputs are both tested at startup to ensure they meet miniLock’s requirements.
If either input is unacceptable your callback will receive one of the following error strings:
"Can’t make keys without a secret phrase."
"Can’t make keys because secretPhrase is not an acceptable secret phrase."
"Can’t make keys without an email address."
"Can’t make keys because emailAddress is not an acceptable email address."
If boths inputs are OK your callback will receive a set of keys when the operation is complete.
It typically takes two-to-three seconds to complete a key pair operation.
Special Extras
miniLockLib is composed of code from several dandy projects.
All of these fine modules are exposed so that you can easily apply their functions in your own computer programs.
miniLockLib’s BLAKE2s constructor accepts a params object instead of two distinct arguments.
Construct a new BLAKE2s instance of with code in this form: hash = new miniLockLib.BLAKE2s({length:Number, key:Uint8Array}).
The length parameter should be a Number between 1 and 32.
The key parameter is an optional Uint8Array of bytes.
Update the instance with hash.update(data) where data is a Uint8Array of bytes.
miniLockLib’s version of update returns the hash instance to facilitate method chaining. (Dmitry’s version returns undefined).
miniLockLib.NaCl is the tweetnacl-js crypto library written by Dmitry Chestnykh & Devi Mandiri.
This instance of NaCl is extended with Dmitry’s streaming encryption library which is defined at NaCl.stream.
miniLockLib relies on NaCl for a host of cryptographic and encoding functions.
scrypt
miniLockLib.scrypt is a copy of scrypt-async.js
from the scrypt-async-js project
written by Mr. Chestnykh once again.
scrypt derives a key pair from a secret and a salt with the curve25519 encryption scheme.
miniLockLib relies on scrypt to make key pairs.