query
On this page

convertHash

builtins.convertHash

Primop
Docs pulled from | This Revision | 1 day ago

Takes 1 arguments

args


Return the specified representation of a hash string, based on the attributes presented in args:

  • hash

    The hash to be converted. The hash format is detected automatically.

  • hashAlgo

    The algorithm used to create the hash. Must be one of

    • "md5"
    • "sha1"
    • "sha256"
    • "sha512"

    The attribute may be omitted when hash is an SRI hash or when the hash is prefixed with the hash algorithm name followed by a colon. That <hashAlgo>:<hashBody> syntax is supported for backwards compatibility with existing tooling.

  • toHashFormat

    The format of the resulting hash. Must be one of

    • "base16"
    • "nix32"
    • "base32" (deprecated alias for "nix32")
    • "base64"
    • "sri"

The result hash is the toHashFormat representation of the hash hash.

Example

Convert a SHA256 hash in Base16 to SRI:

builtins.convertHash {
  hash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
  toHashFormat = "sri";
  hashAlgo = "sha256";
}
"sha256-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU="

Example

Convert a SHA256 hash in SRI to Base16:

builtins.convertHash {
  hash = "sha256-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=";
  toHashFormat = "base16";
}
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"

Example

Convert a hash in the form <hashAlgo>:<hashBody> in Base16 to SRI:

builtins.convertHash {
  hash = "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
  toHashFormat = "sri";
}
"sha256-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU="
This function is not defined in a .nix file. It is likely a builtins function or an alias of a builtins function. builtins functions are predefined functions provided by Nix.

Implementation

This function is implemented in c++ and is part of the native nix runtime.