Sha1 and Sha2 cleanups & optimizations, take 2

Same content as #8097, but bors had an issue with that pull request. Opening a new one.