├── .gitignore ├── go.mod ├── go.sum ├── .github ├── dependabot.yaml └── workflows │ └── ci.yml ├── Makefile ├── .goreleaser.yaml ├── snapcraft └── snap │ └── snapcraft.yaml ├── LICENSE ├── main.go └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | **/*.snap 3 | 4 | bcrypt-tool 5 | output/ 6 | dist/ 7 | .DS_Store 8 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/shoenig/bcrypt-tool 2 | 3 | go 1.25 4 | 5 | require golang.org/x/crypto v0.42.0 6 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | golang.org/x/crypto v0.42.0 h1:chiH31gIWm57EkTXpwnqf8qeuMUi0yekh6mT2AvFlqI= 2 | golang.org/x/crypto v0.42.0/go.mod h1:4+rDnOTJhQCx2q7/j6rAN5XDw8kPjeaXEUR2eL94ix8= 3 | -------------------------------------------------------------------------------- /.github/dependabot.yaml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: gomod 4 | directory: "/" 5 | schedule: 6 | interval: "monthly" 7 | labels: 8 | - "theme/dependencies" 9 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: run-ci 2 | on: [push] 3 | jobs: 4 | run-tests: 5 | runs-on: ubuntu-22.04 6 | steps: 7 | - uses: actions/checkout@v4 8 | - uses: hashicorp/setup-golang@v3 9 | with: 10 | version-file: go.mod 11 | - name: Vet 12 | run: | 13 | make vet 14 | - name: Tests 15 | run: | 16 | make test 17 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | NAME = bcrypt-tool 2 | 3 | .PHONY: build 4 | build: clean 5 | CGO_ENABLED=0 go build -o output/$(NAME) 6 | 7 | .PHONY: clean 8 | clean: 9 | rm -rf dist output/$(NAME) 10 | 11 | .PHONY: test 12 | test: 13 | go test -race ./... 14 | 15 | .PHONY: vet 16 | vet: 17 | go vet ./... 18 | 19 | .PHONY: release 20 | release: 21 | envy exec gh-release goreleaser release --clean 22 | $(MAKE) clean 23 | 24 | default: build 25 | -------------------------------------------------------------------------------- /.goreleaser.yaml: -------------------------------------------------------------------------------- 1 | before: 2 | hooks: 3 | - go mod tidy 4 | builds: 5 | - env: 6 | - CGO_ENABLED=0 7 | goos: 8 | - linux 9 | - darwin 10 | - windows 11 | - freebsd 12 | - openbsd 13 | - plan9 14 | checksum: 15 | name_template: 'checksums.txt' 16 | snapshot: 17 | name_template: "{{ incpatch .Version }}-next" 18 | changelog: 19 | sort: asc 20 | filters: 21 | exclude: 22 | - '^docs:' 23 | - '^test:' 24 | -------------------------------------------------------------------------------- /snapcraft/snap/snapcraft.yaml: -------------------------------------------------------------------------------- 1 | name: bcrypt-tool 2 | base: core20 3 | version: 'v1.1.1' 4 | summary: bcrypt-tool is a dandy CLI tool for generating and matching bcrypt hashes 5 | description: | 6 | bcrypt-tool is a dandy CLI tool for generating and matching bcrypt hashes 7 | 8 | grade: stable 9 | confinement: strict 10 | architectures: 11 | - build-on: amd64 12 | 13 | apps: 14 | bcrypt-tool: 15 | command: bin/bcrypt-tool 16 | 17 | parts: 18 | bcrypt-tool: 19 | plugin: go 20 | go-channel: 1.16/stable 21 | source: https://github.com/shoenig/bcrypt-tool 22 | source-type: git 23 | source-tag: v1.1.1 24 | build-packages: 25 | - git 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Seth Hoenig 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | // Command bcrypt-tool is command line tool for common bcrypt functions 2 | // including the ability to generate hashes, determine if a password 3 | // matches a hash, and compute cost from a hash. 4 | package main 5 | 6 | import ( 7 | "fmt" 8 | "os" 9 | "strconv" 10 | 11 | "golang.org/x/crypto/bcrypt" 12 | ) 13 | 14 | const ( 15 | helpText = `Usage: bcrypt-tool [action] argument ... 16 | ACTIONS 17 | hash [password] Generate hash given password and optional cost (4-31) 18 | match [password] [hash] Print "yes" and return 0 if password is a match 19 | for hash, or print "no" and return 1 otherwise 20 | cost [hash] Print the cost of hash (4-31)` 21 | ) 22 | 23 | func main() { 24 | os.Args = os.Args[1:] 25 | 26 | if len(os.Args) < 2 { 27 | help() 28 | } 29 | switch os.Args[0] { 30 | case "cost": 31 | if len(os.Args) != 2 { 32 | help() 33 | } 34 | c := cost(os.Args[1]) 35 | fmt.Println(fmt.Sprintf("%d", c)) 36 | case "match": 37 | if len(os.Args) != 3 { 38 | help() 39 | } 40 | ok := match(os.Args[1], os.Args[2]) 41 | if ok { 42 | fmt.Println("yes") 43 | } else { 44 | fmt.Println("no") 45 | os.Exit(1) 46 | } 47 | case "hash": 48 | if len(os.Args) > 4 { 49 | help() 50 | } 51 | passwd := os.Args[1] 52 | cost := bcrypt.DefaultCost 53 | if len(os.Args) == 3 { 54 | c, e := strconv.Atoi(os.Args[2]) 55 | if e != nil { 56 | help() 57 | } 58 | cost = c 59 | } 60 | hash := hash(passwd, cost) 61 | fmt.Println(hash) 62 | default: 63 | help() 64 | } 65 | } 66 | 67 | func help() { 68 | _, _ = fmt.Fprintln(os.Stderr, helpText) 69 | os.Exit(2) 70 | } 71 | 72 | func cost(hash string) int { 73 | h := []byte(hash) 74 | c, e := bcrypt.Cost(h) 75 | if e != nil { 76 | _, _ = fmt.Fprintln(os.Stderr, e) 77 | os.Exit(2) 78 | } 79 | return c 80 | } 81 | 82 | func match(password, hash string) bool { 83 | p := []byte(password) 84 | h := []byte(hash) 85 | e := bcrypt.CompareHashAndPassword(h, p) 86 | if e != nil { 87 | return false 88 | } 89 | return true 90 | } 91 | 92 | func hash(password string, cost int) string { 93 | p := []byte(password) 94 | h, e := bcrypt.GenerateFromPassword(p, cost) 95 | if e != nil { 96 | _, _ = fmt.Fprintln(os.Stderr, e) 97 | os.Exit(2) 98 | } 99 | return string(h) 100 | } 101 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | bcrypt-tool 2 | =========== 3 | 4 | `bcrypt-tool` is a dandy CLI tool for generating and matching bcrypt hashes 5 | 6 | [![GitHub](https://img.shields.io/github/license/shoenig/bcrypt-tool.svg)](LICENSE) 7 | 8 | # Project Overview 9 | 10 | Module `github.com/shoenig/bcrypt-tool` provides a simple command line tool. 11 | 12 | # Getting Started 13 | 14 | #### Install from Releases 15 | 16 | The `bcrypt-tool` tool is available from the [Releases](https://github.com/shoenig/bcrypt-tool/releases) page. 17 | 18 | It is pre-compiled for many operating systems and architectures including 19 | 20 | - Linux 21 | - Windows 22 | - MacOS 23 | - FreeBSD 24 | - OpenBSD 25 | - Plan9 26 | 27 | #### Install from SnapCraft 28 | 29 | [![Get it from the Snap Store](https://snapcraft.io/static/images/badges/en/snap-store-white.svg)](https://snapcraft.io/bcrypt-tool) 30 | 31 | The `bcrypt-tool` command can be installed as a snap 32 | ```bash 33 | $ sudo snap install bcrypt-tool 34 | ``` 35 | 36 | #### Build from source 37 | The `bcrypt-tool` command can be compiled by running 38 | ```bash 39 | $ go get github.com/shoenig/bcrypt-tool 40 | ``` 41 | 42 | ### Examples 43 | 44 | #### Generate Hash from a Password 45 | ```bash 46 | $ bcrypt-tool hash p4ssw0rd 47 | ``` 48 | 49 | #### Generate Hash from a Password with Cost 50 | ```bash 51 | $ bcrypt-tool hash p4ssw0rd 31 52 | ``` 53 | 54 | #### Determine if Password matches Hash 55 | ```bash 56 | $ bcrypt-tool match p4ssw0rd $2a$10$nWFwjoFo4zhyVosdYMb6XOxZqlVB9Bk0TzOvmuo16oIwMZJXkpanW 57 | ``` 58 | 59 | note: depending on your shell, you may need to escape the $ characters 60 | 61 | #### Determine Cost of Hash 62 | ```bash 63 | $ bcrypt-tool cost $2a$10$nWFwjoFo4zhyVosdYMb6XOxZqlVB9Bk0TzOvmuo16oIwMZJXkpanW 64 | ``` 65 | 66 | note: depending on your shell, you may need to escape the $ characters 67 | 68 | ### Usage 69 | ```bash 70 | bcrypt-tool [action] parameter ... 71 | ``` 72 | 73 | #### Actions 74 | 75 | - `hash [password] ` Use bcrypt to generate a hash given password and optional cost (4-31) 76 | 77 | - `match [password] [hash]` Use bcrypt to check if a password matches a hash 78 | 79 | - `cost [hash]` Use bcrypt to determine the cost of a hash (4-31) 80 | 81 | # Contributing 82 | 83 | The `github.com/shoenig/bcrypt-tool` module is always improving with new features 84 | and error corrections. For contributing bug fixes and new features please file an issue. 85 | 86 | # License 87 | 88 | The `github.com/shoenig/bcrypt-tool` module is open source under the [MIT](LICENSE) license. 89 | --------------------------------------------------------------------------------