forked from xeodou/go-sqlcipher
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from ValentinMontmirail/encryption
Put back test to ensure encryption.
- Loading branch information
Showing
4 changed files
with
52,135 additions
and
59,867 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package sqlite3 | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"os" | ||
) | ||
|
||
// sqlite3Header defines the header string used by SQLite 3. | ||
var sqlite3Header = []byte("SQLite format 3\000") | ||
|
||
// IsEncrypted returns true, if the database with the given filename is | ||
// encrypted, and false otherwise. | ||
// If the database header cannot be read properly an error is returned. | ||
func IsEncrypted(filename string) (bool, error) { | ||
// open file | ||
db, err := os.Open(filename) | ||
if err != nil { | ||
return false, err | ||
} | ||
defer db.Close() | ||
// read header | ||
var header [16]byte | ||
n, err := db.Read(header[:]) | ||
if err != nil { | ||
return false, err | ||
} | ||
if n != len(header) { | ||
return false, errors.New("go-sqlcipher: could not read full header") | ||
} | ||
// SQLCipher encrypts also the header, the file is encrypted if the read | ||
// header does not equal the header string used by SQLite 3. | ||
encrypted := !bytes.Equal(header[:], sqlite3Header) | ||
return encrypted, nil | ||
} |
Oops, something went wrong.