sha-1 sha-256_什么是SHA-256,以及如何用不同的编程语言计算SHA-256?

SHA-256是一种加密哈希函数,由NSA设计,用于生成256位的不可逆哈希值。文章介绍了SHA-256的主要特点,并在Python、Go、PHP、JavaScript和PowerShell中展示了如何计算SHA-256。
摘要由CSDN通过智能技术生成
sha-1 sha-256

sha-1 sha-256

Hash algorithms are the heart of the cryptography and security. SHA-256 is a Secure Hash Algorithm which will generate an output hash value in 256 bit. SHA-256 is designed by the National Security Agency  (NSA). SHA-256 is one of the cryptographic hash functions. SHA-256 has also named a one-way function where the generated hash value cannot be reversed theoretically. This makes SHA-256 very useful for password validation, challenge hash authentication, anti-tamper, digital signatures, X.509, SSL/TLS certificates, etc.

哈希算法是加密和安全性的核心。 SHA-256是一种安全哈希算法,它将生成256位的输出哈希值。 SHA-256由国家安全局(NSA)设计。 SHA-256是加密哈希函数之一。 SHA-256还命名为单向函数,其中生成的哈希值在理论上不能逆转。 这使得SHA-256对于密码验证,质询哈希认证,防篡改,数字签名,X.509,SSL / TLS证书等非常有用。

SHA-256加密功能 (SHA-256 Cryptographic Features)

As SHA-256 is a cryptographic function it provides some features for cryptographic and security operations.

由于SHA-256是一种加密功能,因此它提供了一些用于加密和安全操作的功能。

  • SHA-256 generated or calculated data will be One-Way. This means it can not be reversed back to the original data with a function. There is an exception where brute-force or rainbow tables can be used.

    SHA-256生成或计算的数据将为“ One-Way 。 这意味着不能使用功能将其恢复为原始数据。 有一个例外,可以使用蛮力表或彩虹表。

  • SHA-256 is a member of the SHA-2 cryptographic hash functions family which is created by NSA.

    SHA-256是由NSA创建的SHA-2加密哈希函数系列的成员。
  • SHA-256 is an anti-tamper feature which means calculating the hash of the same data at different times will provide the same hash value. If there is a single bit change in the data the hash value will be very different. So we can decide the tamper with this feature.

    SHA-256是一种防篡改功能,这意味着在不同时间计算同一数据的哈希将提供相同的哈希值。 如果数据中只有一个位更改,则哈希值将非常不同。 因此,我们可以决定对此功能进行篡改。
  • SHA-256 can be used in challenge handshake authentication because the password is not transmitted in cleartext.

    SHA-256可用于challenge handshake authentication因为密码不是以明文形式传输的。

  • SHA-256 can be used in digital signatures in order to check the given data validity and integrity with authenticity.

    SHA-256可用于digital signatures中,以检查给定数据的有效性和真实性。

在Python中为给定文本生成SHA-256 (Generate SHA-256 For Given Text In Python)

In Python programming language SHA-256 is provided by the hashlib module. We will use sha256() function. In order to provide a string to calculate the hash, we should encode the string with the encode() function like below. We will print the calculated hash value in hexadecimal format with the hexdigest() function.

在Python编程语言中, hashlib模块提供了SHA-256。 我们将使用sha256()函数。 为了提供一个字符串来计算哈希值,我们应该使用如下的encode()函数对字符串进行encode() 。 我们将使用hexdigest()函数以十六进制格式打印计算出的哈希值。

import hashlib
hashvalue=hashlib.sha256("mysecretpassword".encode())
print(hashvalue.hexdigest())
Generate SHA-256 For Given Text In Python
Generate SHA-256 For Given Text In Python
在Python中为给定文本生成SHA-256

We can see that we have calculated two data hash values. These data is very similar where the first one is mysecretpassword and the second one is mysecretpasswrt where only the last letter is different. This changes the calculated hash value completely.

我们可以看到我们已经计算了两个数据哈希值。 这些数据非常相似,其中第一个是mysecretpassword ,第二个是mysecretpasswrt ,仅最后一个字母不同。 这将完全更改计算出的哈希值。

LEARN MORE  What is Checksum?
了解更多什么是校验和?

在Go中为给定文本生成SHA-256 (Generate SHA-256 For Given Text In Go)

Go is a new generation programming language where SHA-256 is supported. We can use the sha256 module Sum256() function by providing the data.

Go是支持SHA-256的新一代编程语言。 通过提供数据,我们可以使用sha256模块的Sum256()函数。

s := "mysecretpassword"

sha256 := sha256.Sum256([]byte(s))

fmt.Printf("%x\n", sha256)

在PHP中为给定文本生成SHA-256 (Generate SHA-256 For Given Text In PHP)

PHP is a web and server-side scripting and programming language. We can use hash() function by providing the data we can to calculate its hash.  We will also provide the hash algorithm we want to use which is sha256 in this case.

PHP是一种Web和服务器端脚本和编程语言。 我们可以通过提供可以计算其哈希值的数据来使用hash()函数。 在这种情况下,我们还将提供我们要使用的哈希算法sha256

$hashvalue=hash('sha256','mysecretpassword');
echo $hashvalue;
Generate SHA-256 For Given Text In PHP
Generate SHA-256 For Given Text In PHP
在PHP中为给定文本生成SHA-256

在JavaScript中为给定文本生成SHA-256 (Generate SHA-256 For Given Text In JavaScript)

JavaScript programming language does not provide built-in SHA-256 algorithm support. So we have to find some external or third-party libraries in order to calculate SHA-256 in JavaScript scripts. The following JavaScript library can be used to calculate SHA-256.

JavaScript编程语言不提供内置的SHA-256算法支持。 因此,我们必须找到一些外部或第三方库才能在JavaScript脚本中计算SHA-256。 以下JavaScript库可用于计算SHA-256。

https://geraintluff.github.io/sha256/

https://geraintluff.github.io/sha256/

var hashvalue=sha256('mysecretpassword')

在PowerShell中为给定文本生成SHA-256 (Generate SHA-256 For Given Text In PowerShell)

PowerShell provides the HashAlgorithms library in order to implement SHA256. We will use Create() function and provide the hash algorithm name which is SHA256.

PowerShell提供HashAlgorithms库以实现SHA256。 我们将使用Create()函数并提供哈希算法名称SHA256

$StringBuilder = New-Object System.Text.StringBuilder

[System.Security.Cryptography.HashAlgorithm]::Create("SHA256").ComputeHash([System.Text.Encoding]::UTF8.GetBytes("mysecretpassword"))|%{

[Void]$StringBuilder.Append($_.ToString("x2"))

}

$StringBuilder.ToString()

在Java中为给定文本生成SHA-256 (Generate SHA-256 For Given Text In Java)

Java programming language provides the SHA-256 for a long time. We will use MessageDigest library in order to create an SHA-256 hash object. Then we will use digest() function in order to calculate the hash of the given text. Calculated hash value will be stored into the byte array named encodedhash.

Java编程语言长期以来一直提供SHA-256。 我们将使用MessageDigest库来创建SHA-256哈希对象。 然后,我们将使用digest()函数来计算给定文本的哈希值。 计算出的哈希值将存储到名为encodedhash的字节数组中。

MessageDigest digest = MessageDigest.getInstance("SHA-256");

byte[] encodedhash = digest.digest("mysecretpassword".getBytes(StandardCharsets.UTF_8));

翻译自: https://www.poftut.com/what-is-sha256-and-how-to-calculate-in-python-php-javascript-go-java-powershell-programming-languages/

sha-1 sha-256

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值