diff --git a/lib/spack/spack/spec.py b/lib/spack/spack/spec.py
index 37a3cf4d7ec090f7ce0d3ca7413f0f30f3ab0c5e..69500255026c7c5577f3e6275dd36e241c62f26d 100644
--- a/lib/spack/spack/spec.py
+++ b/lib/spack/spack/spec.py
@@ -974,10 +974,10 @@ def dag_hash(self, length=None):
yaml_text = syaml.dump(
self.to_node_dict(), default_flow_style=True, width=maxint)
sha = hashlib.sha1(yaml_text)
- b32_hash = base64.b32encode(sha.digest()).lower()[:length]
+ b32_hash = base64.b32encode(sha.digest()).lower()
if self.concrete:
self._hash = b32_hash
- return b32_hash
+ return b32_hash[:length]
def dag_hash_bit_prefix(self, bits):
"""Get the first <bits> bits of the DAG hash as an integer type."""