Skip to content

Image building support does not zero-pad digest hashes to 64 characters #23132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
scottfrederick opened this issue Aug 28, 2020 · 1 comment
Closed
Assignees
Labels
type: bug A general bug
Milestone

Comments

@scottfrederick
Copy link
Contributor

scottfrederick commented Aug 28, 2020

Split from #23130

public static LayerId ofSha256Digest(byte[] digest) {
	Assert.notNull(digest, "Digest must not be null");
	Assert.isTrue(digest.length == 32, "Digest must be exactly 32 bytes");
	String algorithm = "sha256";
	String hash = String.format("%32x", new BigInteger(1, digest));
	return new LayerId(algorithm + ":" + hash, algorithm, hash);
}

%32x is actually only guaranteed to be at least 32 characters. We need 64. So sometimes we get 63 because the content of the digest has a low leading byte value. This would probably work (because Java Formatter pads with spaces not zeros, and docker wants zeros):

String hash = String.format("%64x", new BigInteger(1, digest)).replace(" ", "0");
@scottfrederick scottfrederick self-assigned this Aug 28, 2020
@scottfrederick scottfrederick added the type: bug A general bug label Aug 28, 2020
@scottfrederick scottfrederick added this to the 2.3.x milestone Aug 28, 2020
@scottfrederick scottfrederick changed the title Ensure digest hashes are zero padded Ensure digest hashes are zero padded to 64 characters Aug 28, 2020
@philwebb
Copy link
Member

Looks like we should include a '0' flag. I think "%032x" might work.

@scottfrederick scottfrederick modified the milestones: 2.3.x, 2.3.4 Aug 28, 2020
@wilkinsona wilkinsona changed the title Ensure digest hashes are zero padded to 64 characters Image building support does not zero-pad digest hashes to 64 characters Sep 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants