Skip to content

Instantly share code, notes, and snippets.

@sjenning
Last active December 21, 2022 17:05
Show Gist options
  • Save sjenning/c44cc63e8c0c20bc2b37e8e6b6e4a1a8 to your computer and use it in GitHub Desktop.
Save sjenning/c44cc63e8c0c20bc2b37e8e6b6e4a1a8 to your computer and use it in GitHub Desktop.
Bitwarden JSON encoder
package main
import (
"bufio"
"encoding/json"
"log"
"os"
"strings"
)
// https://bitwarden.com/help/condition-bitwarden-import/#condition-a-json
type BitWardenItems struct {
Items []BitWardenItem `json:"items,omitempty"`
}
const BitWardenLoginType = 1
type BitWardenItem struct {
Type int `json:"type,omitempty"`
Name string `json:"name,omitempty"`
Login BitWardenLogin `json:"login,omitempty"`
}
type BitWardenLogin struct {
Password string `json:"password,omitempty"`
}
func main() {
// import file is one account per line with account name then password separated by a space
file, err := os.Open("pass-export.csv")
if err != nil {
log.Fatal(err)
}
defer file.Close()
var output BitWardenItems
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.Split(scanner.Text(), " ")
output.Items = append(output.Items, BitWardenItem{
Type: BitWardenLoginType,
Name: line[0],
Login: BitWardenLogin{
Password: line[1],
},
})
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
jsonBytes, err := json.Marshal(output)
if err != nil {
log.Fatal(err)
}
err = os.WriteFile("bitwarden-import.csv", jsonBytes, 0644)
if err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment