tcp节点之间 数据传输
In this article, we will explain a few well known Cryptographic Primitives that ensures integrity, authenticity, and non-repudiation in data transmission using node.js.
在本文中,我们将解释一些众所周知的加密原语,这些原语可确保使用node.js进行数据传输时的完整性,真实性和不可否认性。
Integrity: can the recipient be confident that the message has not been modified during its lifecycle?
完整性:收件人能否确定邮件在其生命周期中没有被修改?
Authentication: can the recipient be confident that the message was originated from the sender?
身份验证:收件人可以确定邮件是发件人发出的吗?
Non-repudiation: if the recipient passes the message and the proof to a third party, can the third party be confident that the message was originated from the sender?
不可否认性:如果收件人将消息和证明传递给第三方,那么第三方可以确定消息是发件人发出的吗?
Availability: the information must be available when it is needed. This concept will not be covered by this article.
可用性:信息在需要时必须可用。 本文将不涵盖此概念。
These concepts are also called Security Goals when we want to apply them to our systems. We can achieve these goals by using Cryptographic Primitives.
当我们要将这些概念应用于系统时,这些概念也称为安全目标。 我们可以通过使用加密基元来实现这些目标。
Cryptographic primitives are well-established, low-level cryptographic algorithms that are frequently used to build cryptographic protocols for computer security systems. These routines include but are not limited to, one-way hash functions and encryption functions.
加密原语是公认的低级加密算法,通常用于为计算机安全系统建立加密协议。 这些例程包括但不限于单向哈希函数和加密函数。
In the table below, we can see the Security Goals that some Cryptographic Primitives can provide. In this article, I will be covering examples of HMAC and Digital Signatures.
在下表中,我们可以看到某些加密基元可以提供的安全目标。 在本文中,我将介绍HMAC和数字签名的示例。
A Message Authentication Code (MAC) is a short piece of information used to authenticate a message. In other words, it’s used to confirm that the message came from an expected sender and has not been changed without your knowledge. The MAC value ensures both the integrity and authenticity of a message, by regenerating it in the recipient using a shared secret key (K).
消息验证码(MAC)是用于验证消息的一小段信息。 换句话说,它用于确认邮件来自预期的发件人,并且在您不知情的情况下未被更改。 MAC值通过使用共享密钥(K)在收件人中重新生成消息来确保消息的完整性和真实性 。
A Keyed-Hash Message Authentication Code (HMAC) is a MAC obtained by running a cryptographic hash function (like MD5, SHA1 or SHA256) over the data and a shared secret key.
密钥哈希消息认证码 (HMAC)是通过对数据和共享密钥运行密码哈希函数(例如MD5,SHA1或SHA256)而获得的MAC。
The main difference between MAC and HMAC is that MAC is a tag or a piece of information that helps to authenticate a message, while HMAC is a special type of MAC with a cryptographic hash function and a secret key.
MAC和HMAC之间的主要区别在于,MAC是有助于身份验证消息的标签或信息,而HMAC是具有加密哈希功能和秘密密钥的特殊类型的MAC。
HMACs are almost similar to digital signatures. Both enforce integrity and authenticity. Both use cryptographic keys, and both use hash functions. The main difference is that digital signatures use asymmetric keys, while HMACs use symmetric keys.
HMAC几乎类似于数字签名。 两者都要求完整性和真实性 。 两者都使用加密密钥,并且都使用哈希函数。 主要区别在于数字签名使用非对称密钥,而HMAC使用对称密钥。
HMACs may not enforce non-repudiation because the sender and the receiver share the same secret key.
HMAC可能不会强制执行不可否认性,因为发送者和接收者共享相同的密钥。
A digital signature is created with a private key and verified with the corresponding public key of an asymmetric key-pair. Only the holder of the private key can create this signature.
使用私钥创建数字签名,并使用非对称密钥对的相应公钥验证数字签名。 只有私钥的持有者才能创建此签名。
The public keys are available to everyone. The private key is known only by the owner and can’t be derived from a public key. When something is encrypted with the public key, only the corresponding private key can decrypt it. In addition, when something is encrypted with the private key, then anyone can verify it with the corresponding public key.
公钥对所有人开放。 私钥仅由所有者知道,不能从公钥派生。 用公用密钥加密某些内容时,只有相应的专用密钥才能对其解密。 此外,当使用私有密钥加密某些内容时,任何人都可以使用相应的公共密钥对其进行验证。
Due to some HMAC’s properties (especially its cryptographic strength), it’s highly dependent on its underlying hash function, a particular HMAC is usually identified based on that hash function. So we have HMAC algorithms that go by the names of HMAC-MD5, HMAC-SHA1, or HMAC-SHA256. This last one is cryptographically stronger than the others, so in this article, I’ll provide an example of HMAC using the SHA256 algorithm. See it below:
由于HMAC的某些特性(尤其是其加密强度),它高度依赖于其基础哈希函数,因此通常会基于该哈希函数来识别特定的HMAC。 因此,我们有HMAC算法,它们的名称分别为HMAC-MD5,HMAC-SHA1或HMAC-SHA256。 最后一个在密码上比其他的要强,因此在本文中,我将提供一个使用SHA256算法的HMAC示例。 在下面看到它:
This will generate the HMAC of the body object with the sharedSecret
这将使用sharedSecret生成body对象的sharedSecret
npm run get_hmac// $ HMAC generated: <COPY_THIS_HMAC>This will validate the HMAC provided by the get_hmac method. Try changing the provided HMAC, the body or the sharedSecret and see what happens!
这将验证get_hmac方法提供的HMAC。 尝试更改提供的HMAC, body或sharedSecret ,看看会发生什么!
npm run --hmac=<PASTE_THE_HMAC_HERE> validate_hmac// $ Valid HMAC | Error: ...Apparently, just comparing the two hashes as strings would be enough, but you should compare the time of generation of hashes in order to avoid timing attacks. For more information about this sort of issue, see Coda Hale’s blog post about the timing attacks on KeyCzar and Java’s MessageDigest.isEqual().
显然,仅将两个哈希作为字符串进行比较就足够了,但是您应该比较哈希的生成时间,以避免定时攻击。 有关此类问题的更多信息,请参见Coda Hale的博客文章,内容涉及对KeyCzar和Java的MessageDigest.isEqual()的定时攻击。
Suppose that a client is sending a message to a server, but the message or the hash was modified during its transmission, configuring a man-in-the-middle attack. In this case, the server should detect the possible attack and reject the request.
假设客户端正在向服务器发送消息,但是消息或哈希在其传输过程中被修改,从而配置了中间人攻击 。 在这种情况下,服务器应检测到可能的攻击并拒绝该请求。
This code is also available on this github repo.
此代码也可以在此github repo上找到 。
Digital Signatures work with asymmetric key-pair, one private and others public. In the example below, we will be generating the keys with OpenSSL.
数字签名使用非对称密钥对,一个是私有的,另一个是公共的。 在下面的示例中,我们将使用OpenSSL生成密钥。
Create a folder for your project and create a /keys folder on its root. After that, generate the key-pair. You can follow some simple examples in the lines below:
为您的项目创建一个文件夹,并在其根目录下创建一个/keys文件夹。 之后,生成密钥对。 您可以在以下各行中遵循一些简单的示例:
Creating a private key on the keys folder
在keys文件夹上创建私钥
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:4096 -pkeyopt rsa_keygen_pubexp:3 -out private_key.pemCreating a public key on the keys folder
在keys文件夹上创建一个公共密钥
openssl pkey -in private_key.pem -out public_key.pem -puboutCreate some document in the project root (follow an artistic example below)
在项目根目录中创建一些文档(按照下面的艺术示例)
IMITATIONby Edgar Allan PoeA dark unfathomed tideOf interminable prideA mystery, and a dream,Should my early life seem;I say that dream was fraughtWith a wild and waking thoughtOf beings that have been,Which my spirit hath not seen,Had I let them pass me by,With a dreaming eye!Let none of earth inheritThat vision on my spirit;Those thoughts I would control,As a spell upon his soul:For that bright hope at lastAnd that light time have past,And my wordly rest hath goneWith a sigh as it passed on:I care not though it perishWith a thought I then did cherish.The code below is responsible for creating a digital signature from your document using your private key.
下面的代码负责使用私钥从您的文档创建数字签名。
The code below is responsible for validating the authenticity, integrity, and non-repudiation of your document using your public key.
下面的代码负责使用公钥验证文档的真实性,完整性和不可否认性。
Now, let’s play!
现在,让我们玩吧!
This will write the signature of your document in the file signature.txt
这会将您文档的签名写在文件signature.txt中
npm run sign// $ Digital Signature: ...This will verify the signature of your document. Try changing the content of the document or the signature and see what happens!
这将验证您的文档签名。 尝试更改文档或签名的内容,看看会发生什么!
npm run verify// $ Digital Signature Verification: (true or false)You can take a look at these codes on my github repo.
您可以在我的github repo上查看这些代码。
This depends on the context. Sometimes you will communicate between only two servers on a private network, so a simple HMAC validation might be enough. Sometimes your system will be shared by an uncountable number of clients and servers, so in this case, you might be interested in enforcing non-repudiation, instead of just sharing the secret key with anyone.
这取决于上下文。 有时您只能在专用网络上的两台服务器之间进行通信,因此简单的HMAC验证就足够了。 有时,您的系统将被无数的客户端和服务器共享,因此在这种情况下,您可能有兴趣实施不可否认性,而不仅仅是与任何人共享密钥。
Hopefully, this explanation above will help you implement some best practices of Security Information in your systems using node.js. Additionally, you should be aware that there are many kinds of advanced attacks that should be prevented by using other security layers and strategies.
希望上面的解释可以帮助您使用node.js在系统中实现一些安全信息的最佳实践。 此外,您应该意识到,应使用其他安全层和策略来防止许多高级攻击。
Keep reading post likes this to stay safe.
请继续阅读喜欢的帖子,以确保安全。
https://en.wikipedia.org/wiki/Message_authentication_codehttps://codahale.com/a-lesson-in-timing-attacks/https://en.wikipedia.org/wiki/Cryptographic_hash_functionhttps://www.jscape.com/blog/what-is-hmac-and-how-does-it-secure-file-transfershttps://www.jscape.com/blog/bid/84422/Symmetric-vs-Asymmetric-Encryptionhttps://www.cloudflare.com/learning/security/threats/man-in-the-middle-attack/https://resources.infosecinstitute.com/non-repudiation-digital-signature/#gref
https://en.wikipedia.org/wiki/Message_authentication_code https://codahale.com/a-lesson-in-timing-attacks/ https://en.wikipedia.org/wiki/Cryptographic_hash_function https:// www。 jscape.com/blog/what-is-hmac-and-how-does-it-secure-file-transfers https://www.jscape.com/blog/bid/84422/Symmetric-vs-Asymmetric-Encryption https: //www.cloudflare.com/learning/security/threats/man-in-the-middle-attack/ https://resources.infosecinstitute.com/non-repudiation-digital-signature/#gref
翻译自: https://medium.com/geekoffee/ensuring-integrity-authenticity-and-non-repudiation-in-data-transmission-using-node-js-af73c2404153
tcp节点之间 数据传输
相关资源:微信小程序源码-合集6.rar