Skip to content

Commit 0cb4cf7

Browse files
committed
add a way to try sqlpage.hash_password online
1 parent 3fe3a88 commit 0cb4cf7

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
select 'text' as component, '
2+
3+
# Password Hashing
4+
5+
In SQLPage, you can use the [`sqlpage.hash_password`](/functions.sql?function=hash_password) function to
6+
create a sequence of letters and numbers that can be used to verify
7+
a password, but cannot be used to recover the password itself.
8+
This is called a [hash](https://en.wikipedia.org/wiki/Hash_function) of the password,
9+
and it is a common way to store passwords in a database.
10+
This way, even if someone gains access to the database, they cannot
11+
recover the passwords.
12+
13+
They could still try to guess the passwords, but since SQLPage
14+
uses the [argon2](https://en.wikipedia.org/wiki/Argon2) algorithm,
15+
it would take a very long time (multiple years) to guess a strong password.
16+
17+
The `sqlpage.hash_password` function takes a password as input, and
18+
returns a hash of the password as output. It takes some time
19+
(a few hundred milliseconds) to compute the hash, so you should
20+
only call it when the user is creating a new account and on the initial
21+
login. You should not call it on every page load.
22+
23+
# Try it out
24+
25+
You can try the password hashing function out by entering a password
26+
below and clicking "Hash Password".
27+
' as contents_md;
28+
29+
select 'form' as component, 'Hash Password' as validate;
30+
select 'password' as type, 'password' as name, 'Password' as label, 'Enter a password to hash' as placeholder;
31+
32+
select 'text' as component, '
33+
34+
### Hashed Password
35+
36+
The password you entered above hashed to the following value:
37+
38+
```sql
39+
' || sqlpage.hash_password(:password) || '
40+
```
41+
' as contents_md
42+
where :password is not null;

examples/official-site/sqlpage/migrations/07_authentication.sql

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,20 @@ VALUES (
5757
The most basic usage of the authentication component is to let SQLPage handle the authentication through HTTP basic authentication.
5858
This is the simplest way to password-protect a page, but it is not very user-friendly, because the browser will show an unstyled popup asking for the username and password.
5959
The username and password entered by the user will be accessible in your SQL code using the
60-
[`sqlpage.basic_auth_username()`](functions.sql?function=basic_auth_username) and
61-
[`sqlpage.basic_auth_password()`](functions.sql?function=basic_auth_password) functions.
60+
[`sqlpage.basic_auth_username()`](functions.sql?function=basic_auth_username#function) and
61+
[`sqlpage.basic_auth_password()`](functions.sql?function=basic_auth_password#function) functions.
6262
63-
The [`sqlpage.hash_password`](functions.sql?function=hash_password) function can be used to generate a secure password hash that you need to store in your database.
63+
The [`sqlpage.hash_password`](functions.sql?function=hash_password#function) function can be used to
64+
[generate a secure password hash](/examples/hash_password.sql) that you need to store in your database.
6465
6566
```sql
6667
SELECT ''authentication'' AS component,
6768
''$argon2id$v=19$m=16,t=2,p=1$TERTd0lIcUpraWFTcmRQYw$+bjtag7Xjb6p1dsuYOkngw'' AS password_hash, -- generated using sqlpage.hash_password
6869
sqlpage.basic_auth_password() AS password; -- this is the password that the user entered in the browser popup
6970
```
7071
72+
You can [try the hash_password function out here](/examples/hash_password.sql).
73+
7174
### Usage with a login form
7275
7376
The most basic usage of the authentication component is to simply check if the user has sent the correct password, and if not, redirect them to a login page:

examples/official-site/sqlpage/migrations/08_functions.sql

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,9 @@ VALUES (
139139
'hash_password',
140140
'0.7.2',
141141
'spy',
142-
'Hashes a password using the [Argon2](https://en.wikipedia.org/wiki/Argon2) algorithm.
143-
The resulting hash can be stored in the database and then used with the [authentication component](documentation.sql?component=authentication#component).
142+
'
143+
Hashes a password using the [Argon2](https://en.wikipedia.org/wiki/Argon2) algorithm.
144+
The resulting hash can be stored in the database and then used with the [authentication component](documentation.sql?component=authentication#component).
144145
145146
### Example
146147
@@ -151,6 +152,10 @@ SELECT ''password'' AS name, ''password'' AS type;
151152
152153
INSERT INTO users (name, password_hash) VALUES (:username, sqlpage.hash_password(:password));
153154
```
155+
156+
### Try online
157+
158+
You can try the password hashing function [on this page](/examples/hash_password.sql).
154159
'
155160
);
156161
INSERT INTO sqlpage_function_parameters (

0 commit comments

Comments
 (0)