public static byte[] MD5(byte[] bytes) { using (var provider = MD5provider.Create()) { return(provider.ComputeHash(bytes)); } }
public static KnownHost fromReg(String name) { var rval = new KnownHost(); rval.source = FROM_REGISTRY; var match = regKeyNameRegex.Match(name); if (!match.Success) { rval.parseError = true; return(rval); } rval.keyType = match.Groups[1].Value; rval.port = int.Parse(match.Groups[2].Value); rval.host = match.Groups[3].Value; rval.windowsRegistryName = name; rval.windowsRegistryValue = (String)rkeySshHostKeys.GetValue(name); var decodedParts = DecodePuttyRegPubKey(rval.windowsRegistryValue); rval.exponentBytes = decodedParts.Item1; rval.modulusBytes = decodedParts.Item2; // NOTE pubKeyFormatted is analogous to opensshBase64KeyStr var pubKeyFormatted = makeKeyFingerprint("ssh-rsa", rval.exponentBytes, rval.modulusBytes); using (MD5 md5Hash = MD5.Create()) { rval.keyHash = GetMd5Hash(md5Hash, pubKeyFormatted); } return(rval); }
public static byte[] MD5(byte[] buffer) { using (var md5 = SSCMD5.Create()) { var hash = md5.ComputeHash(buffer); Array.Clear(buffer, 0, buffer.Length); return(hash); } }
public static string MD5(string str) { using (var provider = MD5provider.Create()) { byte[] data = provider.ComputeHash(Encoding.UTF8.GetBytes(str)); return(Utilities.ToHEX(data)); } }
/// <summary> /// MD5 Hash /// </summary> /// <param name="text"></param> /// <returns></returns> public static byte[] MD5Hash(string text) { ThrowHelper.ThrowIfNull(text, "text"); var buffer = UTF8Encoding.GetBytes(text); using (var hashAlgorithm = MD5Crypt.Create()) { return(hashAlgorithm.ComputeHash(buffer)); //16 * 8 = 128 } }
/// <summary> /// md5加密 /// </summary> /// <param name="source">源字符串</param> /// <param name="type">加密类型[16位/32位/48位/64位],默认32位</param> /// <returns>string</returns> public static string MD5(string source, int type = 32) { var result = string.Empty; try { var bytes = SysMD5.Create().ComputeHash(Encoding.UTF8.GetBytes(source)); var sb = new StringBuilder(); foreach (var item in bytes) { //加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位 switch (type) { case 16: case 32: sb.Append(item.ToString("x2")); break; case 48: sb.Append(item.ToString("x3")); break; case 64: sb.Append(item.ToString("x4")); break; } } result = sb.ToString().ToUpper(); if (type == 16) { result = result.Substring(8, 16); } } catch (Exception ex) { LogHelper.Error(ex, "MD5加密"); } return(result); }
/// <summary> /// Encrypts the given <see cref="string"/> with MD5. /// </summary> /// <param name="value">The <see cref="string"/>.</param> /// <returns>The encrypted <see cref="string"/>.</returns> public static string EncryptMD5(this string value) { return(Encrypt(value, string.Empty, MD5.Create())); }
/// <summary>Creates an instance of the default implementation of the <see cref="T:System.Security.Cryptography.MD5" /> hash algorithm.</summary> /// <returns>A new instance of the <see cref="T:System.Security.Cryptography.MD5" /> hash algorithm.</returns> /// <exception cref="T:System.Reflection.TargetInvocationException">The algorithm was used with Federal Information Processing Standards (FIPS) mode enabled, but is not FIPS compatible.</exception> /// <PermissionSet> /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode" /> /// </PermissionSet> public new static MD5 Create() { return(MD5.Create("System.Security.Cryptography.MD5")); }
public static Boolean MD5(ReadOnlySpan <Byte> data, Span <Byte> destination, out Int32 written) { using Md5 md5 = Md5.Create(); return(md5.TryComputeHash(data, destination, out written)); }
public static Byte[] MD5(Byte[] data) { using Md5 md5 = Md5.Create(); return(md5.ComputeHash(data)); }
public static KnownHost fromOpenSshKnownHosts(string line) { /* for details on openssh known_hosts format, see: * http://man.openbsd.org/sshd.8#SSH_KNOWN_HOSTS_FILE_FORMAT */ var rval = new KnownHost(); rval.opensshLine = line; var pieces = line.Split(' ').Where(s => s.Length > 0).ToArray(); string marker = null, hostnames, comment = null; string b64KeyFingerprint; switch (pieces.Length) { case 3: hostnames = pieces[0]; rval.keyType = pieces[1]; b64KeyFingerprint = pieces[2]; break; case 4: if (pieces[0][0] == '@') { marker = pieces[0]; hostnames = pieces[1]; rval.keyType = pieces[2]; b64KeyFingerprint = pieces[3]; } else { hostnames = pieces[0]; rval.keyType = pieces[1]; b64KeyFingerprint = pieces[2]; comment = pieces[3]; } break; case 5: marker = pieces[0]; hostnames = pieces[1]; rval.keyType = pieces[2]; b64KeyFingerprint = pieces[3]; comment = pieces[4]; break; default: throw new Exception("invalid openssh-format known_hosts line: wrong number of space-separated fields"); } if (marker != null && marker[0] != '@') { throw new Exception("invalid openssh-format known_hosts line: markers must begin with '@'"); } /* "hostnames" is probably hashed */ if (hostnames[0] == '|') { var hostHashPieces = hostnames.Split('|'); if (hostHashPieces.Length != 4) { throw new Exception(@"found ""|"" but not the right number"); } rval.hostHashSalt = Convert.FromBase64String(hostHashPieces[2]); rval.hostHash = Convert.FromBase64String(hostHashPieces[3]); } else { rval.host = hostnames; } /* Extract public key */ var decodedKeyParts = DecodeOpenSSHPubKey(b64KeyFingerprint); rval.exponentBytes = decodedKeyParts.Item1; rval.modulusBytes = decodedKeyParts.Item2; /* Calculate checksum hash */ /* TODO this is a little more work than it has to be */ var pubKeyFormatted = makeKeyFingerprint("ssh-rsa", rval.exponentBytes, rval.modulusBytes); using (MD5 md5Hash = MD5.Create()) { rval.keyHash = GetMd5Hash(md5Hash, pubKeyFormatted); } return(rval); }
/// <summary> /// Returns an MD5 Hash that matches the deprecated MD5 Hash /// </summary> public static string GetMD5Hash(this string value) { using (var hash = MD5.Create()) return(string.Join("", hash.ComputeHash(Encoding.UTF8.GetBytes(value)).Select(b => b.ToString("X2")))); }