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.

Revisions

  1. sjenning revised this gist Dec 21, 2022. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,8 @@ import (
    "strings"
    )

    // https://bitwarden.com/help/condition-bitwarden-import/#condition-a-json

    type BitWardenItems struct {
    Items []BitWardenItem `json:"items,omitempty"`
    }
  2. sjenning created this gist Dec 21, 2022.
    62 changes: 62 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    package main

    import (
    "bufio"
    "encoding/json"
    "log"
    "os"
    "strings"
    )

    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)
    }
    }