Mercurial > cgi-bin > hgweb.cgi > PassMan
comparison src/main/kotlin/name/blackcap/passman/Entry.kt @ 12:a38a2a1036c3
Add import subcommand.
author | David Barts <n5jrn@me.com> |
---|---|
date | Sun, 22 Jan 2023 09:22:53 -0800 |
parents | c69665ff37d0 |
children | 3a3067ba673b |
comparison
equal
deleted
inserted
replaced
11:c69665ff37d0 | 12:a38a2a1036c3 |
---|---|
1 package name.blackcap.passman | 1 package name.blackcap.passman |
2 | 2 |
3 import java.sql.ResultSet | |
3 import java.util.* | 4 import java.util.* |
4 import kotlin.reflect.KProperty | 5 import kotlin.reflect.KProperty |
5 import kotlin.reflect.full.declaredMemberProperties | 6 import kotlin.reflect.full.declaredMemberProperties |
6 | 7 |
7 class Entry(val name: String, val username: String, val password: CharArray, val notes: String?, | 8 class Entry(val name: String, val username: String, val password: CharArray, val notes: String?, |
28 password = _genPassword(length, allowSymbols, verbose), | 29 password = _genPassword(length, allowSymbols, verbose), |
29 notes = _getNotes() | 30 notes = _getNotes() |
30 ) | 31 ) |
31 } | 32 } |
32 | 33 |
34 fun fromDatabase(db: Database, name: String): Entry? { | |
35 db.connection.prepareStatement("select name, username, password, notes, created, modified, accessed from passwords where id = ?").use { | |
36 it.setLong(1, db.makeKey(name)) | |
37 val results = it.executeQuery() | |
38 if (!results.next()) { | |
39 return null | |
40 } | |
41 return Entry( | |
42 name = results.getDecryptedString(1, db.encryption)!!, | |
43 username = results.getDecryptedString(2, db.encryption)!!, | |
44 password = results.getDecrypted(3, db.encryption)!!, | |
45 notes = results.getDecryptedString(4, db.encryption), | |
46 created = results.getDate(5), | |
47 modified = results.getDate(6), | |
48 accessed = results.getDate(7) | |
49 ) | |
50 } | |
51 } | |
52 | |
33 private fun _genPassword(length: Int, allowSymbols: Boolean, verbose: Boolean): CharArray { | 53 private fun _genPassword(length: Int, allowSymbols: Boolean, verbose: Boolean): CharArray { |
34 val generated = generate(length, allowSymbols) | 54 val generated = generate(length, allowSymbols) |
35 if (verbose) { | 55 if (verbose) { |
36 printPassword(generated) | 56 printPassword(generated) |
37 } | 57 } |
43 private fun _getUsername() = mustReadLine("Username: ") | 63 private fun _getUsername() = mustReadLine("Username: ") |
44 | 64 |
45 private fun _getPassword() = mustGetPassword("Password: ", verify = true) | 65 private fun _getPassword() = mustGetPassword("Password: ", verify = true) |
46 | 66 |
47 private fun _getNotes() = readLine("Notes: ") | 67 private fun _getNotes() = readLine("Notes: ") |
68 } | |
69 | |
70 fun insert(db: Database) { | |
71 db.connection.prepareStatement("insert into passwords (id, name, username, password, notes, created, modified, accessed) values (?, ?, ?, ?, ?, ?, ?, ?)") | |
72 .use { | |
73 it.setLong(1, db.makeKey(name)) | |
74 it.setEncryptedString(2, name, db.encryption) | |
75 it.setEncryptedString(3, username, db.encryption) | |
76 it.setEncrypted(4, password, db.encryption) | |
77 it.setEncryptedString(5, notes, db.encryption) | |
78 it.setLongOrNull(6, created?.time) | |
79 it.setLongOrNull(7, modified?.time) | |
80 it.setLongOrNull(8, accessed?.time) | |
81 it.executeUpdate() | |
82 } | |
83 } | |
84 | |
85 fun update(db: Database) { | |
86 db.connection.prepareStatement("update passwords set name = ?, username = ?, password = ?, notes = ?, created = ?, modified = ?, accessed = ? where id = ?").use { | |
87 it.setEncryptedString(1, name, db.encryption) | |
88 it.setEncryptedString(2, username, db.encryption) | |
89 it.setEncrypted(3, password, db.encryption) | |
90 it.setEncryptedString(4, notes, db.encryption) | |
91 it.setLongOrNull(5, created?.time) | |
92 it.setLongOrNull(6, modified?.time) | |
93 it.setLongOrNull(7, accessed?.time) | |
94 it.setLong(8, db.makeKey(name)) | |
95 it.executeUpdate() | |
96 } | |
48 } | 97 } |
49 | 98 |
50 val modifiedOrCreated get() = modified ?: created!! | 99 val modifiedOrCreated get() = modified ?: created!! |
51 | 100 |
52 fun print(redactPassword: String? = null) { | 101 fun print(redactPassword: String? = null) { |