NET Core 通过扩展方法实现密码字符串加密(Sha256和Sha512)
using System;
using System.Security.Cryptography;
using System.Text;
namespace SPACore.Extensions
{
/// <summary>
/// Extension methods for hashing strings
/// </summary>
public static class HashExtensions
{
static void Main(string[] args)
{
var secretString = Guid.NewGuid().ToString("N");
Console.WriteLine($"Guid:{secretString}");
var wetet = Sha256(secretString);
Console.WriteLine($"Sha256:{wetet}");
var sha512 = Sha512(secretString);
Console.WriteLine($"Sha512:{wetet}");
Console.ReadKey();
}
/// <summary>
/// Creates a SHA256 hash of the specified input.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>A hash</returns>
public static string Sha256(this string input)
{
if (String.IsNullOrEmpty(input)) return string.Empty;
using (var sha = SHA256.Create())
{
var bytes = Encoding.UTF8.GetBytes(input);
var hash = sha.ComputeHash(bytes);
return Convert.ToBase64String(hash);
}
}
/// <summary>
/// Creates a SHA256 hash of the specified input.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>A hash.</returns>
public static byte[] Sha256(this byte[] input)
{
if (input == null)
{
return null;
}
using (var sha = SHA256.Create())
{
return sha.ComputeHash(input);
}
}
/// <summary>
/// Creates a SHA512 hash of the specified input.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>A hash</returns>
public static string Sha512(this string input)
{
if (string.IsNullOrEmpty(input)) return string.Empty;
using (var sha = SHA512.Create())
{
var bytes = Encoding.UTF8.GetBytes(input);
var hash = sha.ComputeHash(bytes);
return Convert.ToBase64String(hash);
}
}
}
}
NET Core 通过扩展方法实现密码字符串加密(Sha256和Sha512)的更多相关文章
- 浅谈使用spring security中的BCryptPasswordEncoder方法对密码进行加密与密码匹配
浅谈使用springsecurity中的BCryptPasswordEncoder方法对密码进行加密(encode)与密码匹配(matches) spring security中的BCryptPass ...
- decimal扩展方法(转换为字符串,去掉末尾的0)
/// <summary> /// 转换为字符串,去掉末尾0 /// </summary> /// <param name="target">被 ...
- .NET 简单的扩展方法使用。
写代码时,我们经常会碰到dll中提供的方法,不够用或者不好用的情况.而且我们也不方便去更改dll本身的源码. 这时候我们可以使用.NET提供的"扩展方法"去解决这个问题. 下面我写 ...
- C# this扩展方法
本文导读:扩展方法被定义为静态方法,但它们是通过实例方法语法进行调用的. 它们的第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为前缀. 扩展方法当然不能破坏面向对象封装的概念,所以 ...
- ES6新增语法和内置对象(let,const, Array/String/Set 扩展方法(解构赋值,箭头函数,剩余参数))
1.let ES6中新增的用于声明变量的关键字. let 声明的变量只在所处于的块级有效. 注意:使用 let 关键字声明的变量才具有块级作用域,var 关键字是不具备这个特点的. 1. 防止循环变量 ...
- linq和扩展方法
c#的扩展方法 1.必须是在一个非嵌套.非泛型的静态类中的静态方法 2.至少一个参数,第一个参数附加this,不能有其他修饰符如out.ref 3.第一个参数不能是指针类型 上面例子是自定义的一个扩展 ...
- .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法
.NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...
- .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类
.NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类 0x00 为什么要引入扩展方法 有的中间件功能比较简单,有的则比较复杂,并且依赖其它组件.除 ...
- JS扩展方法——字符串trim()
转自:http://www.cnblogs.com/kissdodog/p/3386480.html <head> <title>测试JS扩展方法</title> ...
- 解决.net中截取字符串的汉字与数字还有静态扩展方法
转载 http://blog.163.com/cn_dreamgo/blog/static/52679452200961033212407/ 这两天在C#编程中应用到C#代码与C的代码信息交互,但 ...
随机推荐
- DataLeap的Catalog系统近实时消息同步能力优化
更多技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群 摘要 字节数据中台DataLeap的Data Catalog系统通过接收MQ中的近实时消息来同步部分元数据.Apa ...
- Could not autowire. No beans of 'RestTemplate' type found.
解决方案 @Resourceprivate RestTemplate restTemplate;
- MIR7创建预制发票BAPI
1.事务代码MIR7 前台输入采购订单等相关字段进行开票 2.代码实现 调用BAPI:BAPI_INCOMINGINVOICE_PARK创建发票 "--------------------@ ...
- three.js 视频融合
MixVideo.js代码: //视频融合 import * as THREE from '../build/three.module.js'; import { API } from '../js. ...
- Rust Installation with relative Configuration
References Rust Course The Hitchhiker's Guide to Rust mirrors.tuna.tsinghua.edu.cn Rust Crates 拉取慢的解 ...
- AtCoder Beginner Contest 242(C~E)
AB 水题 C - 1111gal password 题意:给出 N(\(2\le N\le 1e6\))求满足以下条件的 \(X\) 的数量,需除以模 (\(998244353\)) $X $ 是 ...
- 云原生体系下 Serverless 弹性探索与实践
Serverless 时代的来临 Serverless 顾名思义,是一种"无服务器"架构,因为屏蔽了服务器的各种运维复杂度,让开发人员可以将更多精力用于业务逻辑设计与实现.在 Se ...
- mongodb导入本地json文件
- Liunx快捷命令(别名)与快捷方式(软/硬链接)
一.快捷命令(别名)-临时生效1.命令:alias 别名='原命令' 2.举例:给检查防火墙的命令设置别名 [root@localhost ~]# alias fhq='firewall-cmd -- ...
- java基础-IO流-day13
目录 1. IO的概念 2. 一个一个字符 完成文件的复制 3. 字节流 4. 转换字节流 5. System.in 7.基本数据类型的数据 8. object的处理 1. IO的概念 计算机内存中的 ...