使用 hashlib sha1 的 python 加密基础知识
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4820043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
basics of python encryption w/ hashlib sha1
提问by adam
I'm struggling to fully understand how encryption works and is coded, particularly with python. I'm just trying to get the basics down and create code in the simplest form.
我正在努力完全理解加密的工作原理和编码方式,尤其是使用 python。我只是想了解基础知识并以最简单的形式创建代码。
I'm going to be passing a userID between two different sites, but obviously I need this to be encrypted with a private key so Website2 knows it came from Website1. This seems to be the code for me: http://docs.python.org/library/hashlib.html#module-hashlib, but it doesn't have very good examples (or maybe I'm in the wrong spot).
我将在两个不同的站点之间传递一个用户 ID,但显然我需要使用私钥对其进行加密,以便网站 2 知道它来自网站 1。这似乎是我的代码:http: //docs.python.org/library/hashlib.html#module-hashlib,但它没有很好的例子(或者我可能在错误的地方)。
The problem I'm having is fully understanding how to encode and decode.
我遇到的问题是完全理解如何编码和解码。
So lets say the shared private key which each website will know is:
所以让我们说每个网站都会知道的共享私钥是:
shared_private_key = "ABCDEF"
And I want Website1 to pass to Website2 the userID of:
我希望网站 1 将以下用户 ID 传递给网站 2:
userID = "123456"
How would Website1 encrypt my userID with the private key in a fashion that the encryption can be sent via HTTP headers, and then have Website2 decrypt and be able to read the userID using the shared private key?
网站 1 如何使用私钥加密我的用户 ID,加密可以通过 HTTP 标头发送,然后让网站 2 解密并能够使用共享私钥读取用户 ID?
I apologize for asking such a basic question, but I'm failing to grasp how this should be done. Thanks.
我很抱歉提出这样一个基本问题,但我无法理解应该如何做到这一点。谢谢。
采纳答案by vz0
The hashlibmodule provides hashing functions. While there is some relation to encryption, once you hash some data you can not go back to get the original data from the hash result.
该hashlib模块提供散列函数。虽然与加密存在某种关系,但一旦对某些数据进行哈希处理,就无法返回从哈希结果中获取原始数据。
Instead of encripting the data you can take a different approach: creating a unique signature using a hash of the data and some secret.
您可以采用不同的方法来代替对数据进行加密:使用数据的散列和一些秘密创建唯一的签名。
shared_private_key = "ABCDEF"
def create_signature(data):
return hashlib.sha1(repr(data) + "," + shared_private_key).hexdigest()
def verify_signature(data, signature):
return signature == create_signature(data)
Finally, you send to the Website 2 the data plus the signature. That way you can be (mostly) sure that no unauthorized person tampered the data.
最后,您将数据和签名发送到网站 2。这样您就可以(基本上)确定没有未经授权的人篡改数据。
回答by Marek Sapota
回答by devoid
What you want is an encryption library not one that just provides hash algorithms. With python's hashliblibrary:
你想要的是一个加密库,而不是一个只提供哈希算法的库。使用 python 的hashlib库:
import hashlib
m = hashlib.sha1()
m.update("The quick brown fox jumps over the lazy dog")
print(m.hexdigest())
Returns: 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12
返回: 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12
Given this hash, it is extremely difficultimpossible(in general) to recover the original message. What you want is a encryption library, which the Python standard library doesn't have. There are plenty of questions related to python cryptography librarieson SO that might be helpful.
鉴于此散列,恢复原始消息是 极其困难的(通常)。你想要的是一个加密库,Python 标准库没有。有很多与SO 上的Python 加密库相关的问题可能会有所帮助。

