diff options
Diffstat (limited to 'guix/scripts/hash.scm')
-rw-r--r-- | guix/scripts/hash.scm | 35 |
1 files changed, 26 insertions, 9 deletions
diff --git a/guix/scripts/hash.scm b/guix/scripts/hash.scm index b8b2158195..9b4f419a24 100644 --- a/guix/scripts/hash.scm +++ b/guix/scripts/hash.scm @@ -1,5 +1,5 @@ ;;; GNU Guix --- Functional package management for GNU -;;; Copyright © 2012, 2013, 2014, 2016, 2017 Ludovic Courtès <ludo@gnu.org> +;;; Copyright © 2012, 2013, 2014, 2016, 2017, 2020 Ludovic Courtès <ludo@gnu.org> ;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org> ;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org> ;;; Copyright © 2018 Tim Gesthuizen <tim.gesthuizen@yahoo.de> @@ -20,12 +20,13 @@ ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. (define-module (guix scripts hash) - #:use-module (guix base32) #:use-module (gcrypt hash) #:use-module (guix serialization) #:use-module (guix ui) #:use-module (guix scripts) #:use-module (guix base16) + #:use-module (guix base32) + #:autoload (guix base64) (base64-encode) #:use-module (ice-9 binary-ports) #:use-module (rnrs files) #:use-module (ice-9 match) @@ -42,17 +43,21 @@ (define %default-options ;; Alist of default option values. - `((format . ,bytevector->nix-base32-string))) + `((format . ,bytevector->nix-base32-string) + (hash-algorithm . ,(hash-algorithm sha256)))) (define (show-help) (display (G_ "Usage: guix hash [OPTION] FILE -Return the cryptographic hash of FILE. - -Supported formats: 'nix-base32' (default), 'base32', and 'base16' ('hex' -and 'hexadecimal' can be used as well).\n")) +Return the cryptographic hash of FILE.\n")) + (newline) + (display (G_ "\ +Supported formats: 'base64', 'nix-base32' (default), 'base32', +and 'base16' ('hex' and 'hexadecimal' can be used as well).\n")) (format #t (G_ " -x, --exclude-vcs exclude version control directories")) (format #t (G_ " + -H, --hash=ALGORITHM use the given hash ALGORITHM")) + (format #t (G_ " -f, --format=FMT write the hash in the given format")) (format #t (G_ " -r, --recursive compute the hash on FILE recursively")) @@ -69,10 +74,19 @@ and 'hexadecimal' can be used as well).\n")) (list (option '(#\x "exclude-vcs") #f #f (lambda (opt name arg result) (alist-cons 'exclude-vcs? #t result))) + (option '(#\H "hash") #t #f + (lambda (opt name arg result) + (match (lookup-hash-algorithm (string->symbol arg)) + (#f + (leave (G_ "~a: unknown hash algorithm~%") arg)) + (algo + (alist-cons 'hash-algorithm algo result))))) (option '(#\f "format") #t #f (lambda (opt name arg result) (define fmt-proc (match arg + ("base64" + base64-encode) ("nix-base32" bytevector->nix-base32-string) ("base32" @@ -139,8 +153,11 @@ and 'hexadecimal' can be used as well).\n")) (force-output port) (get-hash)) (match file - ("-" (port-sha256 (current-input-port))) - (_ (call-with-input-file file port-sha256)))))) + ("-" (port-hash (assoc-ref opts 'hash-algorithm) + (current-input-port))) + (_ (call-with-input-file file + (cute port-hash (assoc-ref opts 'hash-algorithm) + <>))))))) (match args ((file) |