generateKey generates a key at the given path used for the encryption of certain user data. Because user data becomes unrecoverable without these keys, this won't overwrite any existing key, and instead outputs a message.
(path string)
| 49 | // certain user data. Because user data becomes unrecoverable without these |
| 50 | // keys, this won't overwrite any existing key, and instead outputs a message. |
| 51 | func generateKey(path string) error { |
| 52 | // Check if key file exists |
| 53 | if _, err := os.Stat(path); err == nil { |
| 54 | log.Info("%s already exists. rm the file if you understand the consequences.", path) |
| 55 | return nil |
| 56 | } else if !os.IsNotExist(err) { |
| 57 | log.Error("%s", err) |
| 58 | return err |
| 59 | } |
| 60 | |
| 61 | log.Info("Generating %s.", path) |
| 62 | b, err := key.GenerateBytes(key.EncKeysBytes) |
| 63 | if err != nil { |
| 64 | log.Error("FAILED. %s. Run writefreely --gen-keys again.", err) |
| 65 | return err |
| 66 | } |
| 67 | err = os.WriteFile(path, b, 0600) |
| 68 | if err != nil { |
| 69 | log.Error("FAILED writing file: %s", err) |
| 70 | return err |
| 71 | } |
| 72 | log.Info("Success.") |
| 73 | return nil |
| 74 | } |
no test coverage detected