diff --git a/breach_test.go b/breach_test.go index a7855e2..88c2381 100644 --- a/breach_test.go +++ b/breach_test.go @@ -237,3 +237,73 @@ func TestBreachedAccountWithoutTruncate(t *testing.T) { }) } } + +// ExampleBreachApi_Breaches_getAllBreaches is a code example to show how to fetch all breaches from the +// HIBP breaches API +func ExampleBreachApi_Breaches_getAllBreaches() { + hc := New() + bl, _, err := hc.BreachApi.Breaches() + if err != nil { + panic(err) + } + if bl != nil && len(bl) != 0 { + for _, b := range bl { + fmt.Printf("Found breach:\n\tName: %s\n\tDomain: %s\n\tBreach date: %s\n\n", + b.Name, b.Domain, b.BreachDate.Time().Format("Mon, 2. January 2006")) + } + } +} + +// ExampleBreachApi_Breaches_getAllBreachesNoUnverified is a code example to show how to fetch all breaches from the +// HIBP breaches API but ignoring unverified breaches +func ExampleBreachApi_Breaches_getAllBreachesNoUnverified() { + hc := New() + bl, _, err := hc.BreachApi.Breaches() + if err != nil { + panic(err) + } + if bl != nil && len(bl) != 0 { + fmt.Printf("Found %d breaches total.\n", len(bl)) + } + + bl, _, err = hc.BreachApi.Breaches(WithoutUnverified()) + if err != nil { + panic(err) + } + if bl != nil && len(bl) != 0 { + fmt.Printf("Found %d verified breaches total.\n", len(bl)) + } +} + +// ExampleBreachApi_BreachByName is a code example to show how to fetch a specific breach +// from the HIBP breaches API using the BreachByName method +func ExampleBreachApi_BreachByName() { + hc := New() + bd, _, err := hc.BreachApi.BreachByName("Adobe") + if err != nil { + panic(err) + } + if bd != nil { + fmt.Println("Details of the 'Adobe' breach:") + fmt.Printf("\tDomain: %s\n", bd.Domain) + fmt.Printf("\tBreach date: %s\n", bd.BreachDate.Time().Format("2006-01-02")) + fmt.Printf("\tAdded to HIBP: %s\n", bd.AddedDate.String()) + } +} + +// ExampleBreachApi_BreachedAccount is a code example to show how to fetch a list of breaches +// for a specific site/account from the HIBP breaches API using the BreachedAccount method +func ExampleBreachApi_BreachedAccount() { + apiKey := os.Getenv("HIBP_API_KEY") + if apiKey == "" { + panic("A API key is required for this API") + } + hc := New(WithApiKey(apiKey)) + bd, _, err := hc.BreachApi.BreachedAccount("multiple-breaches@hibp-integration-tests.com") + if err != nil { + panic(err) + } + for _, b := range bd { + fmt.Printf("Your account was part of the %q breach\n", b.Name) + } +} diff --git a/examples/breaches-api/all-breaches-nounverified/main.go b/examples/breaches-api/all-breaches-nounverified/main.go deleted file mode 100644 index e051605..0000000 --- a/examples/breaches-api/all-breaches-nounverified/main.go +++ /dev/null @@ -1,25 +0,0 @@ -package main - -import ( - "fmt" - "github.com/wneessen/go-hibp" -) - -func main() { - hc := hibp.New() - bl, _, err := hc.BreachApi.Breaches() - if err != nil { - panic(err) - } - if bl != nil && len(bl) != 0 { - fmt.Printf("Found %d breaches total.\n", len(bl)) - } - - bl, _, err = hc.BreachApi.Breaches(hibp.WithoutUnverified()) - if err != nil { - panic(err) - } - if bl != nil && len(bl) != 0 { - fmt.Printf("Found %d verified breaches total.\n", len(bl)) - } -} diff --git a/examples/breaches-api/all-breaches/main.go b/examples/breaches-api/all-breaches/main.go deleted file mode 100644 index c60685d..0000000 --- a/examples/breaches-api/all-breaches/main.go +++ /dev/null @@ -1,20 +0,0 @@ -package main - -import ( - "fmt" - "github.com/wneessen/go-hibp" -) - -func main() { - hc := hibp.New() - bl, _, err := hc.BreachApi.Breaches() - if err != nil { - panic(err) - } - if bl != nil && len(bl) != 0 { - for _, b := range bl { - fmt.Printf("Found breach:\n\tName: %s\n\tDomain: %s\n\tBreach date: %s\n\n", - b.Name, b.Domain, b.BreachDate.Time().Format("Mon, 2. January 2006")) - } - } -} diff --git a/examples/breaches-api/breach-by-name/main.go b/examples/breaches-api/breach-by-name/main.go deleted file mode 100644 index d0326d5..0000000 --- a/examples/breaches-api/breach-by-name/main.go +++ /dev/null @@ -1,21 +0,0 @@ -package main - -import ( - "fmt" - "github.com/wneessen/go-hibp" -) - -func main() { - hc := hibp.New() - bd, _, err := hc.BreachApi.BreachByName("Adobe") - if err != nil { - panic(err) - } - if bd != nil { - fmt.Println("Details of the 'Adobe' breach:") - fmt.Printf("\tDomain: %s\n", bd.Domain) - fmt.Printf("\tBreach date: %s\n", bd.BreachDate.Time().Format("2006-01-02")) - fmt.Printf("\tAdded to HIBP: %s\n", bd.AddedDate.String()) - - } -} diff --git a/examples/breaches-api/breached-account/main.go b/examples/breaches-api/breached-account/main.go deleted file mode 100644 index a690394..0000000 --- a/examples/breaches-api/breached-account/main.go +++ /dev/null @@ -1,22 +0,0 @@ -package main - -import ( - "fmt" - "github.com/wneessen/go-hibp" - "os" -) - -func main() { - apiKey := os.Getenv("HIBP_API_KEY") - if apiKey == "" { - panic("A API key is required for this API") - } - hc := hibp.New(hibp.WithApiKey(apiKey)) - bd, _, err := hc.BreachApi.BreachedAccount("multiple-breaches@hibp-integration-tests.com") - if err != nil { - panic(err) - } - for _, b := range bd { - fmt.Printf("Your account was part of the %q breach\n", b.Name) - } -} diff --git a/examples/pastes-api/pasted-account/main.go b/examples/pastes-api/pasted-account/main.go deleted file mode 100644 index 4fbe464..0000000 --- a/examples/pastes-api/pasted-account/main.go +++ /dev/null @@ -1,22 +0,0 @@ -package main - -import ( - "fmt" - "github.com/wneessen/go-hibp" - "os" -) - -func main() { - apiKey := os.Getenv("HIBP_API_KEY") - if apiKey == "" { - panic("A API key is required for this API") - } - hc := hibp.New(hibp.WithApiKey(apiKey)) - pd, _, err := hc.PasteApi.PastedAccount("account-exists@hibp-integration-tests.com") - if err != nil { - panic(err) - } - for _, p := range pd { - fmt.Printf("Your account was part of the %q paste\n", p.Title) - } -} diff --git a/examples/pwned-password-api/check-password-withpadding/main.go b/examples/pwned-password-api/check-password-withpadding/main.go deleted file mode 100644 index 62673fa..0000000 --- a/examples/pwned-password-api/check-password-withpadding/main.go +++ /dev/null @@ -1,18 +0,0 @@ -package main - -import ( - "fmt" - "github.com/wneessen/go-hibp" -) - -func main() { - hc := hibp.New(hibp.WithPwnedPadding()) - m, _, err := hc.PwnedPassApi.CheckPassword("test") - if err != nil { - panic(err) - } - if m != nil && m.Count != 0 { - fmt.Printf("Your password with the hash %q was found %d times in the pwned passwords DB\n", - m.Hash, m.Count) - } -} diff --git a/examples/pwned-password-api/check-password/main.go b/examples/pwned-password-api/check-password/main.go deleted file mode 100644 index d45b66d..0000000 --- a/examples/pwned-password-api/check-password/main.go +++ /dev/null @@ -1,18 +0,0 @@ -package main - -import ( - "fmt" - "github.com/wneessen/go-hibp" -) - -func main() { - hc := hibp.New() - m, _, err := hc.PwnedPassApi.CheckPassword("test") - if err != nil { - panic(err) - } - if m != nil && m.Count != 0 { - fmt.Printf("Your password with the hash %q was found %d times in the pwned passwords DB\n", - m.Hash, m.Count) - } -} diff --git a/examples/pwned-password-api/check-sha1/main.go b/examples/pwned-password-api/check-sha1/main.go deleted file mode 100644 index 591b4f3..0000000 --- a/examples/pwned-password-api/check-sha1/main.go +++ /dev/null @@ -1,19 +0,0 @@ -package main - -import ( - "fmt" - "github.com/wneessen/go-hibp" -) - -func main() { - hc := hibp.New() - pwHash := "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" // represents the PW: "test" - m, _, err := hc.PwnedPassApi.CheckSHA1(pwHash) - if err != nil { - panic(err) - } - if m != nil && m.Count != 0 { - fmt.Printf("Your password with the hash %q was found %d times in the pwned passwords DB\n", - m.Hash, m.Count) - } -} diff --git a/password_test.go b/password_test.go index 888cea4..ecb75ab 100644 --- a/password_test.go +++ b/password_test.go @@ -1,6 +1,7 @@ package hibp import ( + "fmt" "testing" ) @@ -63,3 +64,49 @@ func TestPwnedPasswordHash(t *testing.T) { }) } } + +// ExamplePwnedPassApi_CheckPassword is a code example to show how to check a given password +// against the HIBP passwords API +func ExamplePwnedPassApi_CheckPassword() { + hc := New() + m, _, err := hc.PwnedPassApi.CheckPassword("test") + if err != nil { + panic(err) + } + if m != nil && m.Count != 0 { + fmt.Printf("Your password with the hash %q was found %d times in the pwned passwords DB\n", + m.Hash, m.Count) + // Output: Your password with the hash "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" was found 86453 times in the pwned passwords DB + } +} + +// ExamplePwnedPassApi_CheckPassword_withPadding is a code example to show how to check a given password +// against the HIBP passwords API with the WithPadding() option set +func ExamplePwnedPassApi_CheckPassword_withPadding() { + hc := New(WithPwnedPadding()) + m, _, err := hc.PwnedPassApi.CheckPassword("test") + if err != nil { + panic(err) + } + if m != nil && m.Count != 0 { + fmt.Printf("Your password with the hash %q was found %d times in the pwned passwords DB\n", + m.Hash, m.Count) + // Output: Your password with the hash "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" was found 86453 times in the pwned passwords DB + } +} + +// ExamplePwnedPassApi_CheckSHA1 is a code example to show how to check a given password SHA1 hash +// against the HIBP passwords API using the CheckSHA1() method +func ExamplePwnedPassApi_CheckSHA1() { + hc := New() + pwHash := "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" // represents the PW: "test" + m, _, err := hc.PwnedPassApi.CheckSHA1(pwHash) + if err != nil { + panic(err) + } + if m != nil && m.Count != 0 { + fmt.Printf("Your password with the hash %q was found %d times in the pwned passwords DB\n", + m.Hash, m.Count) + // Output: Your password with the hash "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" was found 86453 times in the pwned passwords DB + } +} diff --git a/paste_test.go b/paste_test.go index 519049e..ff17511 100644 --- a/paste_test.go +++ b/paste_test.go @@ -1,6 +1,7 @@ package hibp import ( + "fmt" "os" "testing" ) @@ -41,3 +42,20 @@ func TestPasteAccount(t *testing.T) { }) } } + +// ExamplePasteApi_PastedAccount is a code example to show how to fetch a specific paste +// based on its name from the HIBP pastes API using the PastedAccount() method +func ExamplePasteApi_PastedAccount() { + apiKey := os.Getenv("HIBP_API_KEY") + if apiKey == "" { + panic("A API key is required for this API") + } + hc := New(WithApiKey(apiKey)) + pd, _, err := hc.PasteApi.PastedAccount("account-exists@hibp-integration-tests.com") + if err != nil { + panic(err) + } + for _, p := range pd { + fmt.Printf("Your account was part of the %q paste\n", p.Title) + } +}