Importing code from https://hashlib.codeplex.com/SourceControl/changeset/80389