NAME
Picture::Lock - lock a directory, a file or bytes with pictures and a handful of clicks
VERSION
Version 0.01
SYNOPSIS
use Picture::Lock qw(generate_password validate_password);
my $clicks = [[412, 96], [1310, 540], [88, 901], [960, 333], [1702, 77]];
Picture::Lock->lock(directory => 'Accounts', to => 'Accounts.plock',
image => 'harbour.jpg', clicks => $clicks);
Picture::Lock->unlock(from => 'Accounts.plock', to => 'Accounts',
image => 'harbour.jpg', clicks => $clicks);
Picture::Lock->lock(file => 'notes.txt', to => 'notes.plock',
image => 'harbour.jpg', clicks => $clicks);
my $locked = Picture::Lock->lock(data => $secret,
image => 'harbour.jpg', clicks => $clicks);
my $again = Picture::Lock->unlock(locked => $locked,
image => 'harbour.jpg', clicks => $clicks);
my $stored = generate_password(image => 'harbour.jpg', clicks => $clicks);
my $ok = validate_password($stored, image => 'harbour.jpg', clicks => $given);
DESCRIPTION
This is a work in progress, and things may change from one release to the next until a stable v1.00 is released.
Picture::Lock locks a directory into a single file whose key is an image and an ordered set of clicks on it, and turns the same image and clicks into a stored password a web application can check a login against.
What is locked may also be one file, or bytes that never touch a disk. What opens it may be several pictures in an order, each with clicks of its own. And the pictures may be baked inside the locked file, which then opens with clicks alone.
The lock is in "A LOCKED DIRECTORY" and the stored password in "A PICTURE AS A PASSWORD". What can be imported is in "EXPORTS".
From a shell the same lock is the piclock command. Picture::Lock::Picker shows a picture in a terminal and gives back the clicks that are made on it.
The hash, the password hash and the seal that both are made of are those of Crypt::Symmetric::Primitives: BLAKE2b, Argon2id and XChaCha20-Poly1305. The LZ4 block compression is this module's own and can be used by itself. It is in "THE PARTS".
A picture is drawn, where one is shown to be clicked on, by Image::Pixels. Nothing that locks or unlocks draws one. A picture is a key as the bytes of its file, so no file opens or fails to open by what that module makes of it, and where it is missing everything here but the showing of a picture works as it did.
WHAT A LOCKED FILE RESTS ON
- The picture, byte for byte
-
A picture is a key as the bytes of its file. Keep the file that was used, exactly as it is, and keep a copy of it somewhere that is not beside the locked file. The same picture saved again, resized or stripped of its metadata is another picture. See "The picture".
- The clicks, in their order
-
The picture and the clicks, in their order, are the one way in. They are yours to remember, as a password is.
- Clicks that are yours alone
-
Five clicks on a picture 1920 by 1080 can fall 185,000,000,000,000,000 ways, and every try at a file costs memory and time. Make six or more, away from the places a stranger would try first, and they are a strong key. More pictures and a passphrase each add to it. "What a lock costs" has the numbers.
- What is kept of a directory
-
Names, the contents of files, empty directories, permission bits, the time each thing was last modified, and links as links. What is unlocked belongs to whoever unlocks it: owners and groups, the setuid, setgid and sticky bits, access lists and extended attributes are left out, and two names for one file come back as two files. See "lock".
- What a locked file shows
-
The names, the sizes and the shape of what is locked are inside the seal. From outside a file shows that it is a locked file, about how long its contents are, what a try at it costs, and how many pictures and clicks it takes. A file that has its pictures inside shows them, which is what it is for. See "WHAT IS IN THE CLEAR" in Picture::Lock::Format.
- The source stays where it is
-
Locking reads a directory and leaves it as it was.
removetakes its names away once the locked file has been read back whole. To change for good what opens the contents, lock them again: a rekey changes the one copy it is given. See "lock" and "rekey". - Click in private
-
Make clicks where nobody is watching, as a password is typed.
A LOCKED DIRECTORY
A directory and everything in it becomes one file. The file opens with a picture and an ordered handful of clicks on it. The names in the directory, the sizes of its files and the shape of its tree are all inside the seal.
Picture::Lock->lock(
directory => 'Accounts',
to => 'Accounts.plock',
image => 'harbour.jpg',
clicks => [[412, 96], [1310, 540], [88, 901], [960, 333], [1702, 77]],
);
Picture::Lock->unlock(
from => 'Accounts.plock',
to => 'Accounts',
image => 'harbour.jpg',
clicks => [[415, 99], [1305, 544], [90, 897], [958, 330], [1700, 80]],
);
The picture and the clicks are given as "generate_password" takes them: the picture as image, image_bytes or image_hash with width and height, and the clicks as a list of [x, y] pairs or as text. Everything said under "The picture" and "The clicks" holds here. A passphrase may be added, 1 to 1024 bytes, and then all three are needed to open the file.
The picture is its bytes. Keep the file that was used, exactly as it is. The same picture saved again, resized or stripped of its metadata is another picture, and it is the first that opens the file.
The seven below and "calibrate" are called on the class. Each croaks with what went wrong, and "ERRORS" says what to do about it.
One file, and bytes
Picture::Lock->lock(file => 'notes.txt', to => 'notes.plock', ...);
Picture::Lock->unlock(from => 'notes.plock', to => 'notes.txt', ...);
my $locked = Picture::Lock->lock(data => $secret, ...);
my $secret = Picture::Lock->unlock(locked => $locked, ...);
lock takes one of directory, file and data.
One file is locked with its bytes, its permission bits and the time it was last modified. Its name is not kept, as a directory's own name is not: it unlocks to whatever name to gives it. It is read once, and a file that is longer or newer when its last byte has been read than it was when it was opened stops the lock. A link is not followed.
data is bytes, and nothing is kept of them but the bytes. With no to the locked file is not written anywhere: it is what lock returns, and unlock takes it back as locked. This is for a secret that lives in a column of a table. All of it is in memory, so data is at most 64 MiB.
A locked file says what it holds, and unlock makes that at to: a directory, a file, or for bytes a file that is its owner's alone. With no to what was locked comes back as a string, which only bytes can. A file that holds a directory or one file is then refused, before anything is tried against it.
The bytes pass through perl. A copy that perl made of them is perl's, and is not wiped.
Several pictures
Picture::Lock->lock(directory => 'Accounts', to => 'Accounts.plock',
pictures => [
{ image => 'harbour.jpg', clicks => [[412, 96], [1310, 540], [88, 901]] },
{ image => 'cat.png', clicks => [[40, 60], [300, 220]] },
{ image => 'map.webp', clicks => [[700, 410], [150, 135], [1030, 567], [886, 195]] },
]);
pictures is a list of one to eight pictures, in the order they are to be given again. Each is a hash of image, image_bytes or image_hash with width and height, of clicks, and where a lock is made of radius. An image and clicks at the top of a call are a list of one.
One picture alone takes 4 to 16 clicks. One of several takes 1 to 16, and all of them together take 4 or more, at least three of which are in different places. Clicks on different pictures are in different places. A picture may be in a list twice.
Every picture and every click goes into one key. Nothing is worked out or kept for one picture by itself. A wrong first picture, a wrong last picture and a wrong click on any of them all croak cannot unlock, after the same work, and nothing says which it was. So clicks on three pictures are worth the clicks on each multiplied together, which is what several pictures are for.
The file says, to anyone, how many pictures it takes and how many clicks each takes. It cannot say which picture goes where: anything in it that could would let a stranger try pictures without paying for a try. The order is yours to remember.
Pictures inside the file
Picture::Lock->lock(file => 'notes.txt', to => 'notes.plock',
pictures => \@pictures, bake => 1);
Picture::Lock->unlock(from => 'notes.plock', to => 'notes.txt',
clicks => \@clicks);
my @pictures = Picture::Lock->pictures('notes.plock');
With bake the pictures are put inside the locked file, and it then opens with clicks alone.
A picture inside the file can be seen by anyone who has the file, since it has to be shown before the file is open. Such a file is kept shut by its clicks, and so it asks more of them:
With its pictures inside and no passphrase a file takes 8 clicks or more, where any other takes 4.
Unless it is told how much, it asks for
$Picture::Lock::MEMORY_INSIDEof memory, which is 1 GiB, where any other asks for 256 MiB.A passphrase makes it a file like any other again, for the passphrase is not in it.
Choose pictures that you are content for others to see.
A picture that goes inside is given as image or image_bytes, never as its hash, is at most 32 MiB, and its size must be one that can be read from its own bytes. All of them together are at most 64 MiB. They are the files as they were given, byte for byte: a smaller copy would be another picture.
Such a file takes no image. clicks at the top of a call are then the clicks of every picture one after another, and the file, which knows how many each takes, deals them out. Or they are given picture by picture, as pictures, a list of hashes of clicks alone.
A picture inside is part of the key as any other is: one that was changed is another picture. One that was damaged croaks damaged data, before any click is tried against it.
lock
my $said = Picture::Lock->lock(directory => $dir, to => $file,
image => $path, clicks => $clicks);
Locks $dir into a new file, $file. In the place of directory it takes file or data, and in the place of image and clicks it takes pictures. It also takes passphrase, radius, which is for one picture at the top of the call, memory, passes, lanes, bake and remove, which is for a directory.
Nothing is given its name until it is whole and has been read back:
$filemust not be there, and nor must$file.partial. Nothing is ever written over.The directory is looked over first. A device, a socket, a named pipe or a file that cannot be read stops the lock, each named, before a byte is written.
The file is written as
$file.partial, for its owner alone, and flushed to the disk.It is then opened again and read back whole, with the same picture and the same clicks. This costs a second derivation and cannot be turned off. It is the only proof, before the directory is relied on, that this picture and these clicks open this file.
Only then is it given its name. A name that was taken in the meantime is not written over.
The directory is read and never changed. Any failure removes the partial file. A process that is ended leaves it, and the next lock to that name stops and says it is there.
What is kept is names as bytes, the contents of files, directories that are empty, the permission bits, the time each thing was last modified, and symbolic links as links, which are never followed. What is not kept is the owner and the group, the setuid, setgid and sticky bits, access lists, extended attributes, the fact that two names were one file, and the other times.
A file that changes while it is being read stops the lock, and is named. Close whatever writes to the directory before locking it. A change is known by a file's size and time, and on some systems a file written twice in a few thousandths of a second has the same time for both.
It returns a hash: records, the number of entries with the directory itself among them, bytes, the sum of the sizes of its files, length, the length of the locked file, and told, a list of [what, path] for anything worth saying that did not stop it. A file that had two names is told of as many names, and the locked file itself as left out when it was written inside the directory. For one file and for bytes records is 1 and bytes is how many there are. With data and no to it returns the locked file itself, as a string.
With remove => 1 the directory is taken away afterwards. The locked file is read once more, and a thing in the directory is removed only if it is, at that moment, byte for byte what the locked file holds for it. A file that has changed since, a file that was added, and a directory that still holds something are left where they are and told of. removed and kept in what is returned count both.
remove takes names away. Where what was on the disk has to be beyond recovery as well, keep the directory on a disk that is encrypted as a whole.
unlock
my $said = Picture::Lock->unlock(from => $file, to => $dir,
image => $path, clicks => $clicks);
Unlocks $file into a new directory, $dir, which must not be there. Nor must $dir.partial. It takes passphrase as well. In the place of from it takes locked, a locked file in memory. In the place of image and clicks it takes pictures, or clicks alone for a file that has its pictures inside. What it makes at to is what the file holds, and with no to it returns the bytes that were locked.
The header is opened first. A wrong picture, a wrong click, the right clicks in the wrong order and a wrong passphrase all croak cannot unlock, with nothing made, and cannot be told apart. A click is right when it is within the radius of where it was, on both axes.
The directory is filled as $dir.partial, for its owner alone, and is given its name only when every part of the file has proved sound. A file that is damaged croaks damaged at chunk N, and the partial directory is removed with all that was put in it. The locked file is read and never changed.
Modes and times are as they were. The owner is whoever unlocks. Two names that are one name on this disk, as Notes and notes are on many, stop the unlock and are named.
It returns a hash of records, bytes and holds, which is directory, file or bytes, and of times and told. times is how many things had a time that this system's clock cannot hold and were given the nearest it can. told is a list of ['time', $path] for them, with the empty path for the directory itself and for one file.
Before anything is tried against a file it is asked, in this order, whether it holds what was asked for, whether it takes as many pictures as were given and keeps them where the call supposes, whether each picture was given as many clicks as it takes, and whether a passphrase is expected. Each of those is refused for what it is, and costs nothing: they are all things the file says to anyone.
verify
my $said = Picture::Lock->verify(from => $file, image => $path, clicks => $clicks);
Reads a locked file whole and writes nothing: the header is opened, every part is checked and the tree inside is read to its end. It takes what "unlock" takes but to, croaks as it does, and otherwise returns a hash of records, bytes, length and holds.
With portable => 1 it also looks at every name and every time in the file for what another system would not take as it is, and returns what it found as told, a list of [$word, $path]:
same-
Two names in one directory that are one name where capital and small letters, or two spellings of an accented letter, are not told apart, as on a Mac and on Windows. The entry is
['same', $path, $earlier]. bytes-
A name that is not UTF-8, which a Mac will not make and Windows cannot write.
character-
A name that holds one of
< > : " / \ | ? *or a control character, which Windows allows in no name. ending-
A name that ends in a dot or a space.
device-
CON,PRN,AUX,NUL,COM0toCOM9orLPT0toLPT9, with an extension or without. long-
A path of more than 256 characters.
link-
A link.
time-
A time before 13 December 1901 or after 19 January 2038, which a clock of 32 bits cannot hold.
It reads names and numbers only, so it says the same on every system. What it says nothing of is not thereby promised to open everywhere.
list
my $entries = Picture::Lock->list(from => $file, image => $path, clicks => $clicks);
What a locked file holds, as a list of hashes in the order of the tree. Each has type, which is directory, file or link, path, mode, mtime in whole seconds with nanoseconds beside it, and size. A link has target. The first entry is the directory itself, and its path is the empty string.
The whole file is read and checked before anything is returned. A list from a file that turns out to be cut short would not be a list of it.
A file that holds one file is one entry of type file, with no name. One that holds bytes is one entry of type bytes, with a size and no more.
rekey
Picture::Lock->rekey(file => $file, image => $path, clicks => $old,
new_clicks => $new);
Changes what opens a locked file. Its contents are not rewritten, so it takes the same time for a file of any size.
The old picture, clicks and passphrase are given as for "unlock". The new ones are new_image, new_image_bytes or new_image_hash with new_width and new_height, new_clicks and new_passphrase. A picture or clicks that are not named anew stay as they were. A passphrase does not: none named is none. radius, memory, passes and lanes are for the new header.
The old header is kept beside the file, as $file.header-backup, while the new one is written, and is removed when the new one has been read back. If a rekey is cut off, the next rekey of that file puts the old header back and croaks a rekey that did not finish was undone, and this one was not made. The file then opens as it did before, and the rekey can be run again.
A rekey changes the copy it is given. A copy of the file that was made before it still opens with what opened it then. To change that as well, lock the directory again.
That is the rekey of a directory locked with one picture that stays outside. Every other file, and every other change, is rekeyed another way, because what stands before its contents is not of one length: a new file is made beside the old, as $file.partial, with the contents copied as they are and not sealed again. It is read back whole with the new pictures and clicks and then takes the old file's name in one step. So the name is at every moment the old file or the new one, a rekey that is cut off leaves the old file and at most a $file.partial to remove, and it takes as long as the file is long.
The old pictures and clicks are given as for "unlock". The new are new_pictures, a list as "lock" takes one, or one new image and new_clicks, or nothing, and then they are the old ones. bake says where the pictures go: bake => 1 puts them inside and bake => 0 takes them out, and not said they stay where they are. What the file holds is not changed by any of it.
info
my $said = Picture::Lock->info($file);
my $said = Picture::Lock->info(locked => $bytes);
What a locked file says to anyone, with no picture and no clicks: version, memory, passes, lanes, radius, clicks, which is how many it takes, passphrase, which is true when it expects one, affordable, which is true when its cost is one this version will pay, length and chunks. It says nothing of whether the file is whole, or of what would open it.
It also says holds, which is directory, file or bytes, inside, which is true when the file has its pictures inside it, and pictures, a list with a hash of clicks and radius for each picture the file takes, and of length where the picture is inside. radius and clicks at the top are the first picture's.
pictures
my @pictures = Picture::Lock->pictures($file);
my @pictures = Picture::Lock->pictures(locked => $bytes);
The pictures a locked file has inside it, as their bytes, in order. It takes no picture and no clicks: they are not secret. A file that keeps its pictures outside gives none. It is how a program that asks for clicks in its own way gets what to show.
calibrate
my $found = Picture::Lock->calibrate(seconds => 1);
Picture::Lock->lock(directory => $dir, to => $file, image => $path, clicks => $clicks,
memory => $found->{memory}, passes => $found->{passes});
Tries the machine it runs on. It runs derivations at rising memory until one takes about the time asked, and says the cost that did. It locks nothing and changes nothing.
seconds is a whole number from 1 to 60, or milliseconds one from 10 to 60000: one or the other, and a second where neither is given. passes and lanes are those the file will have, and are the package variables unless they are given. most is the most memory it may ask for, in KiB.
What comes back is a hash.
memory,passes,lanes-
The cost, as "lock" and "rekey" take it. The memory is in KiB, and is a whole number of MiB.
seconds-
What one derivation at that cost took here.
stopped-
Why it stopped where it did.
reached: that cost takes about the time asked.most: the most it may ask takes less than that.memory: the machine gave no more.least: the least a file may ask already takes longer. tries-
How many derivations it ran.
most,free-
The most memory it allowed itself, and how much the system said could be had at once, both in KiB.
freeis undef where the system does not say. usual_seconds-
What the cost in the package variables takes here, or undef where that cost asks for more than
most.
It never asks for more than half of what the system says is free, and where the system does not say, never for more than a GiB. It takes about five times the time asked.
Its tries are made in one process, and all but the first find memory that an earlier one has left warm. A command that opens a file touches its memory for the first time, and takes a little longer than calibrate says: 13 percent longer for a second's worth, on the machine in "What a lock costs".
It suggests. A locked file asks its cost of every machine that opens it, and a cost fitted to a workstation may not open on a laptop. That is why "lock" never calibrates and the default is a fixed number. Choose for the slowest machine that will open the file.
With password true it tries the cost of a stored password: its limits, its package variables, and a tenth of a second where no time is given.
my $found = Picture::Lock->calibrate(password => 1, milliseconds => 50);
It croaks where there is no clock fine enough to time a derivation.
What a lock costs
Each try at a locked file is made to cost memory and time, so that trying many is slow: 256 MiB of memory and three passes over it by default. $Picture::Lock::MEMORY, in KiB, $Picture::Lock::PASSES and $Picture::Lock::LANES are the defaults, and memory, passes and lanes say otherwise for one call. A locked file may ask for 8192 to 4194304 KiB, 1 to 64 passes and 1 to 16 lanes.
A file with its pictures inside and no passphrase asks for $Picture::Lock::MEMORY_INSIDE where it is not told how much, which is 1048576, a GiB.
"lock" pays the cost twice, "unlock", "verify" and "list" once, and "rekey" three times. However many pictures a file takes, a try is one cost.
What a cost takes is the machine's to say. "calibrate" asks it, and piclock calibrate does the same from a command line.
On an Apple M5 with 16 GiB, built by Apple clang 17 for perl 5.42, one try in a process of its own took this many seconds:
memory 1 pass 2 passes 3 passes 4 passes
19 MiB 0.006 0.012 0.016 0.022
64 MiB 0.026 0.046 0.063 0.084
256 MiB 0.114 0.200 0.293 0.369
1024 MiB 0.479 0.947 1.500 1.852
So the default, 256 MiB and three passes, is three tenths of a second there, and a second is about 880 MiB. A process holds the memory of its cost and about 5 MiB more while it tries. They are that machine's figures and no other's.
A try by a process that has made one already is quicker, since the memory is by then its own: 0.259 for the default, and 1.080 for a GiB and three passes. A server that validates logins all day pays that. A command that opens a file pays the table.
At 0.293 seconds a try, one core makes about 295,000 tries a day. Five clicks on a picture 1920 by 1080 can fall 185,000,000,000,000,000 ways, which is some 1,700,000,000 years of that, and each click more multiplies it by 2840. Those are the figures for clicks that could be anywhere on the picture, so choose places that are not the first a stranger would try. More memory makes every try dearer still, and "calibrate" says how much a machine can give.
What a locked file shows of itself, and what its length tells, is in Picture::Lock::Format.
A PICTURE AS A PASSWORD
A web application keeps one printable string for each user where it would keep the hash of a text password. The user logs in by clicking the same points, in the same order, on the same picture.
my $stored = generate_password(
image => '/srv/app/pictures/42.jpg',
clicks => [ [412, 96], [1310, 540], [88, 901], [960, 333], [1702, 77] ],
);
if (validate_password($stored, image => $path, clicks => $given)) {
$stored = rehash_password($stored, image => $path, clicks => $given)
if needs_rehash($stored);
...
}
Read "WHAT A PICTURE PASSWORD IS WORTH" before putting one in front of a user.
The picture
Every function that takes a picture takes it under one of three names, and under one only:
- image
-
The path of the picture's file.
- image_bytes
-
The bytes of that file.
- image_hash
-
The 64 byte hash of those bytes that "picture" gives. With it go
widthandheight, the picture's size in pixels, since a hash says nothing of a size.
Which of the three a value is, is never guessed from what it looks like.
The picture is its file, byte for byte. Keep the file the user enrolled with: the same picture saved again, resized or stripped of its metadata is another picture.
width and height may be given with image or image_bytes as well. They are then held to what the file itself says, and a difference croaks. They must be given when the file is of a kind whose size cannot be read: any bytes at all can be the picture, as long as the application can say how large it draws them.
A PNG, JPEG, GIF, BMP or WebP file has its size read from its own header. The size is the stored one. A JPEG that a camera marked as turned is not turned here, so place clicks on the picture as stored, or turn it for good before it is enrolled.
The clicks
A click is a pixel of the picture as stored: x counted from the left edge and y from the top, both from 0. There are 4 to 16 of them and their order matters.
They are given as a reference to an array of [x, y] pairs, or as text:
412,96 1310,540 88,901 960,333 1702,77
Pairs with one space between them, a comma and no space inside a pair, each number 1 to 8 digits with no sign and no leading zero, nothing before the first pair and nothing after the last. The text form is there so that a picture password can arrive in the one form field, and pass through the one scalar, that a text password uses.
A click is a whole number of pixels. Scale what a browser reports to the stored size and round it before it gets here: a fraction is refused, not rounded, because two programs that round differently would be two different passwords.
A click that lands within the radius of the enrolled one, on both axes, validates. One pixel further does not.
picture
my %picture = picture(image => $path);
my %picture = picture(image_bytes => $bytes);
What an application may keep beside a user so that the file need not be read at every login: image_hash, width, height, format and orientation.
format is png, jpeg, gif, bmp, webp or unknown. width and height are 0 when the size could not be read. orientation is 1 to 8 as a JPEG's Exif block gives it, and 0 when there is none.
The hash is BLAKE2b under a personalisation of its own. It is not the checksum that a general tool prints for the file. It is half of the user's secret where the picture is private, so treat it as the picture is treated.
Croaks if the file cannot be read.
generate_password
my $stored = generate_password(
image => $path,
clicks => $clicks,
radius => 13,
memory => 65536,
passes => 3,
lanes => 1,
);
The string to store for this picture and these clicks. Only the picture and the clicks are required.
radius is how many pixels off a later click may be. It is one of 0, 1, 4, 13, 40, 121, 364, 1093, 3280, 9841 and 29524, and sixteen squares of side 2 * radius + 1 must fit along the picture's longer side. Left out, it is the one of those nearest a hundredth of the longer side: 13 for a picture 1920 pixels wide. A wider radius is easier on the hand, and a narrower one makes each click count for more.
memory, passes and lanes are the cost of checking a guess, as "argon2id" in Crypt::Symmetric::Primitives takes them. Left out they are the three package variables in "What it costs".
Croaks when no string can be made: fewer than 4 clicks or more than 16, a click outside the picture, clicks that are not in at least three different places, clicks that are not clicks, a radius or a cost that is not allowed, a picture that cannot be read, a size that is needed and missing.
Three clicks are in different places when each is more than the radius from both of the others, on one axis or the other. The rule keeps a user from clicking one spot five times.
validate_password
my $ok = validate_password($stored, image => $path, clicks => $clicks);
1 when the clicks are the enrolled ones, each within the radius. The empty string when they are not. Never anything else.
It is false, and does not croak, for everything a user could be the cause of and for everything that could be in the column: clicks that do not match, too few or too many of them, text that is not clicks, clicks that are undef, a stored value that is undef, empty, a reference, the hash of a text password, a string of a later version, a string that asks for more memory than a password may. A row with no password can be handed straight in.
It croaks for what is the application's mistake or the machine's: no picture or more than one, a name it does not know, a hash with no size, a size that is not the picture's, a picture file that cannot be read, memory that cannot be had.
It takes no radius and no cost. They are in the string.
A wrong click costs what a right one costs, and so do a wrong number of clicks and clicks that are not clicks at all. Whoever is guessing learns neither how many there are, nor whether the account is there, from how soon the answer came. A stored value that is not a stored string is answered at once. For the login of a user who does not exist, see "dummy_validate".
needs_rehash
if (needs_rehash($stored)) { ... }
if (needs_rehash($stored, memory => 131072, passes => 3)) { ... }
1 when the string should be made again: its memory or its passes are under those now wanted, or it is not a string this version reads. The empty string otherwise. Lanes play no part.
The cost wanted is the one given, and for what is not given, the package variables. Act on a true answer only after "validate_password" has said yes, with the clicks that were just given.
rehash_password
my $new = rehash_password($stored, image => $path, clicks => $clicks);
my $new = rehash_password($stored, image => $path, clicks => $clicks,
memory => 131072);
A new string for the same clicks at the cost now wanted, under a new salt. Undef when the clicks do not validate against $stored, for any of the reasons "validate_password" is false. It croaks as that function croaks, and for a cost no password may ask.
The points that are accepted do not move. The clicks given only prove who is asking: the new string holds the points first enrolled, however far inside the radius the clicks just given were. An application that enrolled the user afresh at each login instead would let the accepted point drift a little each time.
dummy_validate
dummy_validate();
Does the work of one validation and answers the empty string. Call it when a login names a user who does not exist, so that the answer takes as long as it does for one who does. The cost is the package variables unless memory, passes and lanes are given.
The stored string
$piclock$v=1$m=65536,t=3,p=1,r=13,n=5$SALT$OFFSETS$TAG
One line of printable ASCII, 159 characters for five clicks at the cost above and never more than 301. A text column holds it. It names the cost, the radius and the number of clicks, then a 16 byte salt, 8 bytes for each click and a 32 byte tag, in base64 with the URL-safe alphabet and no padding.
Nothing in it can be changed and leave it valid for the same clicks. It does not hold the picture, the picture's hash or the picture's size.
The eleven bytes the picture, taken as a picture 1920 by 1080, with the five clicks at the head of this section, a radius of 13 and the sixteen bytes 0 to 15 for a salt, give this string. It is one line, broken here to fit the page:
$piclock$v=1$m=65536,t=3,p=1,r=13,n=5$AAECAwQFBgcICQoLDA0ODw$FQAAAAIAAAA
BAAAADgAAABUAAAAYAAAAAgAAABcAAAAPAAAACgAAAA$mTCNveOHDBCRcgu75-fxscwv2kxrc
TP5wY_PSWibZkg
Picture::Lock::Format says how every byte of it is come by.
It does hold the number of clicks, and for each click where it sits inside a square of the grid. That is how a click a few pixels off is still found. It is also why the string is to be guarded as a password hash is: with the string and the picture, each click is known to be one of a few thousand pixels, not one of two million.
What it costs
$Picture::Lock::PASSWORD_MEMORY = 65536;
$Picture::Lock::PASSWORD_PASSES = 3;
$Picture::Lock::PASSWORD_LANES = 1;
The cost of a new string, of a rehash, and of a dummy validation when none is named: 64 MiB, three passes, one lane. A password may ask for 1024 to 1048576 KiB, 1 to 16 passes and 1 to 4 lanes. A stored string that asks for more is not acted on, so whoever can write the column cannot make a login take a gigabyte.
Every validation holds that much memory for as long as it runs, for anyone who posts the form. Nothing here limits how many run at once. The application must.
Picture::Lock->calibrate(password => 1) says what cost takes a tenth of a second on the machine that will run the logins: see "calibrate". On the machine in "What a lock costs", a validation at the default took 0.059 seconds in a process that had made one before, and 0.015 at 19 MiB.
A test suite sets the variables to 1024 and 1.
WHAT A PICTURE PASSWORD IS WORTH
Five clicks on a picture 1920 by 1080, at the radius it gets, can fall about 185,000,000,000,000,000 ways: 2840 squares, five times over. A sixth click makes that 2840 times as many.
Clicks are strongest where they are least expected. Ask for six or more, away from the eyes, the corners and the one bright thing in the picture, and limit attempts as for any password.
The picture adds to the secret wherever it is kept private. Where the application shows a user their picture before they click, the clicks are the whole of the secret, and the figures above are for exactly that.
Keep the picture's file as it was enrolled: see "The picture".
EXPORTS
Nothing is exported unless it is asked for.
use Picture::Lock qw(generate_password validate_password);
use Picture::Lock qw(:password);
use Picture::Lock qw(:parts);
use Picture::Lock qw(:all);
- :password
-
picture,generate_password,validate_password,needs_rehash,rehash_passwordanddummy_validate. - :parts
-
compressanddecompress. - :all
-
Both.
Each can also be called by its full name, as Picture::Lock::generate_password, with nothing imported.
The seven that work on a locked file, "lock", "unlock", "verify", "list", "rekey", "info" and "pictures", and "calibrate", which tries the machine, are called on the class and are not exported. lock is the name of one of perl's own functions.
The importing is done by Ex::Op, which takes what Exporter takes: a tag, a name left out with !, a pattern, a version.
WHAT A FUNCTION TAKES
Bytes
Wherever data, a key, a salt, a nonce, a block or a path is asked for, it is a string of bytes. A string holding a character above 255 croaks with perl's Wide character, so encode text first. undef is the empty string, with perl's usual warning.
An object that turns itself into a string is asked for it, once. Any other reference croaks. Its text would be an address in memory, which as a password is one nobody can give twice.
Whole numbers
Wherever a length, a cost, a size, a radius or a click is asked for, it is a whole number, and nothing is rounded to make it one. 412, 412.0, '412' and '412.0' are 412. 412.5 is not a number here, nor is '412.00000000000000001', nor text with an exponent in it, a word, the empty string, undef or a reference of any kind. Each croaks.
Names
A function that takes names and values croaks for a name it does not know and for a list with a value missing. A name given twice counts once, the last one standing. A value of undef is a name left out.
ERRORS
Every failure is a croak, but for "validate_password" and its kin, which answer false to whatever a user could have caused. Where a name of a file is part of a message it is shown here as NAME, and N is a number.
What was offered does not open the file
Nothing was made, the file is as it was, and the call can be made again.
cannot unlock-
The picture, a click, the order of the clicks, the order of the pictures or the passphrase is not what locked the file. Nothing says which, and nothing can. A click is right within the radius of where it was made, on both axes. A picture that has been saved again since is another picture: find the file as it was.
not the number of clicks this takesnot the pictures this takes: how many there are, or whether it has its own insidea passphrase is expected and none was given, or one was given and none is expectedit does not hold what was asked for: a directory, one file, or bytes-
The file says each of these to anyone, and "info" gives them: how many clicks each picture takes, how many pictures there are and whether they are inside it, whether it expects a passphrase, and what it holds. Each is refused before anything is tried, and costs nothing. A file that holds a directory or one file needs
to.
The file is not what it was
damaged datadamaged at chunk N-
Bytes of the file were changed or lost where it was kept or on its way. The first is the 307 bytes at its head or what stands between them and the contents. The second is the contents, which are sealed 65552 bytes at a time, counted from 0. Fetch another copy of the file. Nothing of a damaged file is left unlocked on the disk.
not made by Picture::Lock-
It is not a locked file, or it is one with its first bytes gone.
made by a later version of Picture::Lock-
A later release wrote it. Install that release to open it.
asks for more memory, passes or lanes than is allowed here-
The cost in the file is outside what this version will pay for a guess, which is in "What a lock costs". No file that "lock" writes says such a cost.
not a tree this version wrotenot a stream this version wrote-
The file is sealed rightly, and what the seal holds is not what this version writes. Another program wrote it, or one that had a fault.
a picture is too large to go inside, or the pictures inside are not what the file says they are
The machine or the disk said no
The system's own words follow a colon where it gave any.
N KiB of memory is needed and could not be had-
The file is whole and the machine cannot pay its cost at this moment. Close what else is running, or open it on a machine with more memory. Where it is a lock that says so, ask for less with
memory. cannot read NAME: ...cannot make NAME: ...cannot write NAME.partial: ...-
A file that is not there, a directory that may not be written in, a disk that is full. Nothing is left half made.
NAME is there already-
Nothing is ever written over. Give another name, or move what is there.
NAME.partial is there: a run that did not finish left it. Look at it, and remove it-
A lock, an unlock or a rekey that was ended before its last step leaves what it was making under that name. It is never the only copy of anything: what it was made from is untouched. Remove it and run again.
two names in it are one name on this disk: ...-
The file holds two names that this disk takes for one, as
Notesandnotesare on many. They are named, and nothing is guessed. Unlock it onto a disk that keeps them apart. a name in it is one this system cannot hold: ...a link in it cannot be made here: ...-
Each is named, and nothing is left of the unlock. A Mac says the first of a name that is not UTF-8, and a disk that has no links, as one in the FAT format has none, says the second on any system. "verify" with
portablesays beforehand what a file holds of either kind. the system gave no random bytes
What cannot be locked
a picture takes 4 to 16 clickslock: a list of pictures takes at least 4 clicks in all, and each picture 1 to 16with its pictures inside and no passphrase a file takes at least 8 clicksat least three clicks must be in different placesa click is outside the imageclicks as text are x,y pairs with one space between them-
The rules of "The clicks", "Several pictures" and "Pictures inside the file".
the size of the image could not be read-
The picture is of a kind whose size is not in its own bytes. Give
widthandheight. the directory holds what cannot be kept: ...-
A device, a socket, a named pipe, a file that cannot be read, a name too long or a tree too deep, each named with what it is. Nothing was written. Move it out of the directory, or make it readable, and lock again.
a file changed while it was being read: NAME-
Something wrote to the directory during the lock. Close it and lock again.
what was written did not read back as what was sealed (...)-
The locked file was written, read back, and was not what had been sealed, for the reason in the brackets. It was removed and nothing was given its name. Lock again. A lock that says this a second time points at the disk or at the memory of the machine.
A rekey
a rekey that did not finish was undone, and this one was not made-
An earlier rekey of this file was cut off. Its old header has been put back, the file opens as it did before that rekey, and this rekey has changed nothing. Run it again.
the header kept beside the file is not a header of this file-
There is a
NAME.header-backupthat does not belong toNAME. Nothing was changed. Look at it, and move it away.
A call that is put together wrongly
A missing argument, a name that is not known, two arguments that cannot go together, a number that is not a whole number or is out of its range: each croaks with the name of the function, what it takes, and nothing done. They are the program's to mend and are not listed one by one. "WHAT A FUNCTION TAKES" has the rules that all of them follow.
THE PARTS
LZ4 block compression, which the lock packs the contents of a file with. The two functions can be used on their own, and each is imported by its name or with :parts.
The hash, the password hash and the seal are not this module's. They are blake2b, argon2id, seal and open_sealed of Crypt::Symmetric::Primitives, which is installed with this one.
compress
my $block = compress($bytes);
Compresses $bytes into one block of the LZ4 block format and returns it.
It is the bare block and nothing more: no frame around it, no checksum, and no record of how long the data was. Whoever keeps the block keeps that length beside it, because "decompress" has to be told.
Data with no repetition in it does not shrink. It grows, by about one byte in every 255: a block is never longer than length + length / 255 + 16 bytes. Compare the two lengths and keep the original when the block is no shorter.
The same bytes give the same block on every machine. A later version of this module may give a different block for them, which will still decompress.
$bytes is bytes. A string holding a character above 255 croaks, and so does one longer than 2113929216 bytes.
decompress
my $bytes = decompress($block, $length);
The bytes $block was made from, or undef.
$length is the length of the original data, exactly. Undef means $block is not one whole LZ4 block that decodes to exactly that many bytes: it is damaged, it is cut short, something follows it, or the length is not the right one. Any bytes at all may be given as a block. The answer is the data or undef. An empty original comes back as the empty string, which is defined, so test the result with defined.
A block written by a conforming LZ4 compressor is accepted. A block that breaks the format's own rules is refused: one whose last five bytes are not plain literals, one whose last match starts less than twelve bytes before the end, and one that reaches back to before the start of the data or by a distance of zero. Other readers let some of those through. The format allows a reader to refuse them, and this one always does.
$length is a whole number, zero or more, or the call croaks. The memory for the result is taken before the block is read, and it can be up to 255 times the length of the block. A length past that, or past 2113929216, is answered with undef and no memory is taken for it, but a caller given a length by someone it does not trust should bound it all the same.
RANDOM BYTES
The salt of a stored password comes from the operating system, through "random_bytes" in Crypt::Symmetric::Primitives, which names the sources and the order they are tried in. If the source fails the call croaks. There is no weaker source to fall back on.
THREADS
Everything here may be called from several threads at once. Nothing is kept between one call and the next, and nothing is shared between two that run together. The package variables of a cost are copied for each thread, as any variable is.
Each call holds the memory of its cost for as long as it runs: four threads that unlock at once hold four times 256 MiB.
Picture::Lock::Picker asks one person at one terminal, and is for one thread at a time.
CALLING FROM C
An extension written in C can call what is under "A PICTURE AS A PASSWORD" with no Perl call in between. It compiles against one header, piclock_abi.h, which is installed with this module and found through ExtUtils::Depends:
my $pkg = ExtUtils::Depends->new('My::Module', 'Picture::Lock');
WriteMakefile(..., $pkg->get_makefile_vars);
The header says how the table of functions is asked for when the extension loads, and what each function takes and answers. "THE C INTERFACE" in Picture::Lock::Format says what a version of the table is and what may change from one to the next.
Nothing called that way reads "What it costs". A caller that names no cost gets 65536 KiB and 3 passes, whatever the package variables say.
The hash, the password hash and the seal are not in that table. An extension that wants them from C asks Crypt::Symmetric::Primitives for its own, in the same way and through a header of its own, symprim_abi.h.
AUTHOR
LNATION <email@lnation.org>
BUGS
Please report any bugs or feature requests to bug-picture-lock at rt.cpan.org, or through the web interface at https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Picture-Lock.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Picture::Lock
LICENSE AND COPYRIGHT
This software is Copyright (c) 2026 by LNATION <email@lnation.org>.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)