Debian Codegraph @

sqlite3_func_crypt.go

raw
1 // Copyright (C) 2018 G.J.R. Timmer <[email protected]>.
2 //
3 // Use of this source code is governed by an MIT-style
4 // license that can be found in the LICENSE file.
5
6 package sqlite3
7
8 import (
9 "crypto/sha1"
10 "crypto/sha256"
11 "crypto/sha512"
12 )
13
14 // This file provides several different implementations for the default
15 // embedded sqlite_crypt function. This function uses a caesar-cypher by
16 // default and is used within the UserAuthentication module to encode the
17 // password.
18 //
19 // The provided functions can be used as an overload to the sqlite_crypt
20 // function through the use of the RegisterFunc on the connection.
21 //
22 // Because the functions can serve a purpose to an end-user without using the
23 // UserAuthentication module the functions are default compiled in.
24 //
25 // From SQLITE3 - user-auth.txt
26 // The sqlite_user.pw field is encoded by a built-in SQL function
27 // "sqlite_crypt(X,Y)". The two arguments are both BLOBs. The first argument is
28 // the plain-text password supplied to the sqlite3_user_authenticate()
29 // interface. The second argument is the sqlite_user.pw value and is supplied
30 // so that the function can extract the "salt" used by the password encoder.
31 // The result of sqlite_crypt(X,Y) is another blob which is the value that ends
32 // up being stored in sqlite_user.pw. To verify credentials X supplied by the
33 // sqlite3_user_authenticate() routine, SQLite runs:
34 //
35 // sqlite_user.pw == sqlite_crypt(X, sqlite_user.pw)
36 //
37 // To compute an appropriate sqlite_user.pw value from a new or modified
38 // password X, sqlite_crypt(X,NULL) is run. A new random salt is selected when
39 // the second argument is NULL.
40 //
41 // The built-in version of of sqlite_crypt() uses a simple Caesar-cypher which
42 // prevents passwords from being revealed by searching the raw database for
43 // ASCII text, but is otherwise trivally broken. For better password security,
44 // the database should be encrypted using the SQLite Encryption Extension or
45 // similar technology. Or, the application can use the
46 // sqlite3_create_function() interface to provide an alternative implementation
47 // of sqlite_crypt() that computes a stronger password hash, perhaps using a
48 // cryptographic hash function like SHA1.
49
50 // CryptEncoderSHA1 encodes a password with SHA1
51 func CryptEncoderSHA1(pass []byte, hash any) []byte {
52 h := sha1.Sum(pass)
53 return h[:]
54 }
55
56 // CryptEncoderSSHA1 encodes a password with SHA1 with the
57 // configured salt.
58 func CryptEncoderSSHA1(salt string) func(pass []byte, hash any) []byte {
59 return func(pass []byte, hash any) []byte {
60 s := []byte(salt)
61 p := append(pass, s...)
62 h := sha1.Sum(p)
63 return h[:]
64 }
65 }
66
67 // CryptEncoderSHA256 encodes a password with SHA256
68 func CryptEncoderSHA256(pass []byte, hash any) []byte {
69 h := sha256.Sum256(pass)
70 return h[:]
71 }
72
73 // CryptEncoderSSHA256 encodes a password with SHA256
74 // with the configured salt
75 func CryptEncoderSSHA256(salt string) func(pass []byte, hash any) []byte {
76 return func(pass []byte, hash any) []byte {
77 s := []byte(salt)
78 p := append(pass, s...)
79 h := sha256.Sum256(p)
80 return h[:]
81 }
82 }
83
84 // CryptEncoderSHA384 encodes a password with SHA384
85 func CryptEncoderSHA384(pass []byte, hash any) []byte {
86 h := sha512.Sum384(pass)
87 return h[:]
88 }
89
90 // CryptEncoderSSHA384 encodes a password with SHA384
91 // with the configured salt
92 func CryptEncoderSSHA384(salt string) func(pass []byte, hash any) []byte {
93 return func(pass []byte, hash any) []byte {
94 s := []byte(salt)
95 p := append(pass, s...)
96 h := sha512.Sum384(p)
97 return h[:]
98 }
99 }
100
101 // CryptEncoderSHA512 encodes a password with SHA512
102 func CryptEncoderSHA512(pass []byte, hash any) []byte {
103 h := sha512.Sum512(pass)
104 return h[:]
105 }
106
107 // CryptEncoderSSHA512 encodes a password with SHA512
108 // with the configured salt
109 func CryptEncoderSSHA512(salt string) func(pass []byte, hash any) []byte {
110 return func(pass []byte, hash any) []byte {
111 s := []byte(salt)
112 p := append(pass, s...)
113 h := sha512.Sum512(p)
114 return h[:]
115 }
116 }
117
118 // EOF