各种语言HMAC SHA256实现

来源:互联网 发布:女权运动 知乎 编辑:程序博客网 时间:2024/06/12 19:09

语言包含:
Javascript ,PHP,Java,Groovy,C#,Objective C,Go,Ruby,Python,Perl,Dart,Swift,Rust,Powershell。

1. Javascript HMAC SHA256

Run the code online with this jsfiddle. Dependent upon an open source js library calledhttp://code.google.com/p/crypto-js/.<script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/hmac-sha256.js"></script><script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/components/enc-base64-min.js"></script><script>  var hash = CryptoJS.HmacSHA256("Message", "secret");  var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);  document.write(hashInBase64);</script>

2. PHP HMAC SHA256

PHP has built ) and base64_encode (PHP , PHP ) resulting in no outside dependencies. Say what you want about PHP but they have the cleanest code for this example.$s = hash_hmac('sha256', 'Message', 'secret', true);echo base64_encode($s);

3. Java HMAC SHA256

Dependent on Apache Commons Codec to encode in base64.import javax.crypto.Mac;import javax.crypto.spec.SecretKeySpec;import org.apache.commons.codec.binary.Base64;public class ApiSecurityExample {  public static void main(String[] args) {    try {     String secret = "secret";     String message = "Message";     Mac sha256_HMAC = Mac.getInstance("HmacSHA256");     SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");     sha256_HMAC.init(secret_key);     String hash = Base64.encodeBase64String(sha256_HMAC.doFinal(message.getBytes()));     System.out.println(hash);    }    catch (Exception e){     System.out.println("Error");    }   }}

4. Groovy HMAC SHA256

It is mostly Java code but there are some slight differences. Adapted from Dev Takeout - Groovy HMAC/SHA256 representation.import javax.crypto.Mac;import javax.crypto.spec.SecretKeySpec;import java.security.InvalidKeyException;def hmac_sha256(String secretKey, String data) { try {    Mac mac = Mac.getInstance("HmacSHA256")    SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), "HmacSHA256")    mac.init(secretKeySpec)    byte[] digest = mac.doFinal(data.getBytes())    return digest   } catch (InvalidKeyException e) {    throw new RuntimeException("Invalid key exception while converting to HMac SHA256")  }}def hash = hmac_sha256("secret", "Message")encodedData = hash.encodeBase64().toString()log.info(encodedData)

5. C# HMAC SHA256

using System.Security.Cryptography;namespace Test{  public class MyHmac  {    private string CreateToken(string message, string secret)    {      secret = secret ?? "";      var encoding = new System.Text.ASCIIEncoding();      byte[] keyByte = encoding.GetBytes(secret);      byte[] messageBytes = encoding.GetBytes(message);      using (var hmacsha256 = new HMACSHA256(keyByte))      {        byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);        return Convert.ToBase64String(hashmessage);      }    }  }}

6. Go programming language - Golang HMAC SHA256

Try it online in your browser with Play GoLangcrypto/hmac packagepackage mainimport (    "crypto/hmac"    "crypto/sha256"    "encoding/base64"    "fmt")func ComputeHmac256(message string, secret string) string {    key := []byte(secret)    h := hmac.New(sha256.New, key)    h.Write([]byte(message))    return base64.StdEncoding.EncodeToString(h.Sum(nil))}func main() {    fmt.Println(ComputeHmac256("Message", "secret"))}

7. Ruby HMAC SHA256

Requires openssl and base64.require 'openssl'require "base64"hash  = OpenSSL::HMAC.digest('sha256', "secret", "Message")puts Base64.encode64(hash)

8. Python (2.7) HMAC SHA256

import hashlibimport hmacimport base64message = bytes("Message").encode('utf-8')secret = bytes("secret").encode('utf-8')signature = base64.b64encode(hmac.new(secret, message, digestmod=hashlib.sha256).digest())print(signature)Tested with Python . Also, be sure not to name your python demo script the same as one of the imported libraries.

9. Perl HMAC SHA256

See Digest::SHA documentation. By convention, the Digest modules . We will use a modulus function below.use Digest::SHA qw(hmac_sha256_base64);$digest = hmac_sha256_base64("Message", "secret");# digest is currently: qnR8UCqJggD55PohusaBNviGoOJ67HC6Btry4qXLVZc# Fix padding of Base64 digests) {    $digest .= '=';}print $digest;# digest is now: qnR8UCqJggD55PohusaBNviGoOJ67HC6Btry4qXLVZc=

10. Dart HMAC SHA256

Dependent upon the Dart crypto package.import 'dart:html';import 'dart:convert';import 'package:crypto/crypto.dart';void main() {  String secret = 'secret';  String message = 'Message';  List<int> secretBytes = UTF8.encode('secret');  List<int> messageBytes = UTF8.encode('Message');  var hmac = new HMAC(new SHA256(), secretBytes);  hmac.add(messageBytes);  var digest = hmac.close();  var hash = CryptoUtils.bytesToBase64(digest);  // output to html page  querySelector('#hash').text = hash;  // hash => qnR8UCqJggD55PohusaBNviGoOJ67HC6Btry4qXLVZc=}

11. Swift HMAC SHA256

I have not verified but see this stackOverflow postRustTake a look at the alco/rust-digest repository for Rust (lang) guidance. I have not verified yet.Powershell (Windows) HMAC SHA256Mostly wrapping of .NET libraries but useful to see it in powershell's befuddling syntax. See code as gist$message = 'Message'$secret = 'secret'$hmacsha = New-Object System.Security.Cryptography.HMACSHA256$hmacsha.key = [Text.Encoding]::ASCII.GetBytes($secret)$signature = $hmacsha.ComputeHash([Text.Encoding]::ASCII.GetBytes($message))$signature = [Convert]::ToBase64String($signature)echo $signature# Do we get the expected signature?echo ($signature -eq 'qnR8UCqJggD55PohusaBNviGoOJ67HC6Btry4qXLVZc=')

转自: https://www.bbsmax.com/A/kPzOa3X15x/

原创粉丝点击