是否有生成HMAC-SHA1
哈希的bash脚本?
我正在寻找与以下PHP代码等效的东西:
hash_hmac("sha1", "value", "key");
Run Code Online (Sandbox Code Playgroud) 我需要在Objective C中生成HMAC-SHA1.但是我找不到任何有用的东西.我尝试使用CCHMAC使用CommonCrypto,但没有用.我需要生成一个hmac并在生成HOTP号后.
有人在Objective C或C中有任何示例代码吗?
所有开发人员都面临的一个非常基本的问题:每当用户提交表单时,密码都会通过网络发送,并且必须受到保护.我开发的网站没有HTTPS.所有者既不想购买SSL证书,也不想对自签名证书感兴趣.所以我想在提交表单时使用Javascript保护通过HTTP发送的密码.
渴望downvoters:如何通过HTTP安全地发送密码?不提供任何合理的解决方案,我处于另一种情况.
如果我使用MD5,可以反转该密码字符串.那怎么样的nonce/HMAC?任何可用的Javascript库吗?或者你有什么建议/提示要解决?提前致谢!
我正在尝试使用HMAC-SHA256算法创建签名,这是我的代码.我正在使用US ASCII编码.
final Charset asciiCs = Charset.forName("US-ASCII");
final Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
final SecretKeySpec secret_key = new javax.crypto.spec.SecretKeySpec(asciiCs.encode("key").array(), "HmacSHA256");
final byte[] mac_data = sha256_HMAC.doFinal(asciiCs.encode("The quick brown fox jumps over the lazy dog").array());
String result = "";
for (final byte element : mac_data)
{
result += Integer.toString((element & 0xff) + 0x100, 16).substring(1);
}
System.out.println("Result:[" + result + "]");
Run Code Online (Sandbox Code Playgroud)
我从上面的代码得到的结果是:
f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8
Run Code Online (Sandbox Code Playgroud)
这与wiki中显示的相同
HMAC_SHA256("key", "The quick brown fox jumps over the lazy dog") = 0x f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8
Run Code Online (Sandbox Code Playgroud)
除了为0x
.
我正在寻找想法/评论,如果我做的一切正确或可能是我可以改进我的代码.
我对消息摘要的理解是,它是与加密数据一起发送的一些数据的加密散列,因此您可以验证数据是否未被篡改.这与消息认证码(MAC)和散列MAC(HMAC)之间有什么区别?
我使用HMAC-SHA1散列一些值,使用Java中的以下代码:
public static String hmacSha1(String value, String key) {
try {
// Get an hmac_sha1 key from the raw key bytes
byte[] keyBytes = key.getBytes();
SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1");
// Get an hmac_sha1 Mac instance and initialize with the signing key
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signingKey);
// Compute the hmac on input data bytes
byte[] rawHmac = mac.doFinal(value.getBytes());
// Convert raw bytes to Hex
byte[] hexBytes = new Hex().encode(rawHmac);
// Covert array of Hex bytes to a String …
Run Code Online (Sandbox Code Playgroud) 我正在寻找通过 Java中的Oauth实现一个获得Twitter授权的应用程序.第一步是获取请求令牌.这是app引擎的Python示例.
为了测试我的代码,我正在运行Python并使用Java检查输出.以下是Python生成基于哈希的消息验证代码(HMAC)的示例:
#!/usr/bin/python
from hashlib import sha1
from hmac import new as hmac
key = "qnscAdgRlkIhAUPY44oiexBKtQbGY0orf7OV1I50"
message = "foo"
print "%s" % hmac(key, message, sha1).digest().encode('base64')[:-1]
Run Code Online (Sandbox Code Playgroud)
输出:
$ ./foo.py
+3h2gpjf4xcynjCGU5lbdMBwGOc=
Run Code Online (Sandbox Code Playgroud)
如何在Java中复制此示例?
try {
// Generate a key for the HMAC-MD5 keyed-hashing algorithm; see RFC 2104
// In practice, you would save this key.
KeyGenerator keyGen = KeyGenerator.getInstance("HmacMD5");
SecretKey key = keyGen.generateKey();
// Create a MAC object …
Run Code Online (Sandbox Code Playgroud) 我正在寻找一个相当于这个php调用的java:
hash_hmac('sha1', "test", "secret")
Run Code Online (Sandbox Code Playgroud)
我试过这个,使用java.crypto.Mac,但两人不同意:
String mykey = "secret";
String test = "test";
try {
Mac mac = Mac.getInstance("HmacSHA1");
SecretKeySpec secret = new SecretKeySpec(mykey.getBytes(),"HmacSHA1");
mac.init(secret);
byte[] digest = mac.doFinal(test.getBytes());
String enc = new String(digest);
System.out.println(enc);
} catch (Exception e) {
System.out.println(e.getMessage());
}
Run Code Online (Sandbox Code Playgroud)
key ="secret"和test ="test"的输出似乎不匹配.
任何人都可以指出使用的优点HM?C
是什么?
例如,如果我有文本T
和密钥K
,我可以使用任一HMAC-MD5
算法或Md5(T + K)
获取签名.
我正在尝试使用C#来使用REST API.API创建者提供了PHP,Ruby和Java的示例库.我被挂在它的一部分,我需要生成一个HMAC
.
以下是它们提供的示例库中的完成方式.
PHP
hash_hmac('sha1', $signatureString, $secretKey, false);
Run Code Online (Sandbox Code Playgroud)
红宝石
digest = OpenSSL::Digest::Digest.new('sha1')
return OpenSSL::HMAC.hexdigest(digest, secretKey, signatureString)
Run Code Online (Sandbox Code Playgroud)
Java的
SecretKeySpec signingKey = new SecretKeySpec(secretKey.getBytes(), HMAC_SHA1_ALGORITHM);
Mac mac = null;
mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
mac.init(signingKey);
byte[] bytes = mac.doFinal(signatureString.getBytes());
String form = "";
for (int i = 0; i < bytes.length; i++)
{
String str = Integer.toHexString(((int)bytes[i]) & 0xff);
if (str.length() == 1)
{
str = "0" + str;
}
form = form + str;
}
return form;
Run Code Online (Sandbox Code Playgroud)
这是我在C#中的尝试.它不起作用.更新:下面的C#示例工作得很好.我发现真正的问题是由于我的换行符中的一些跨平台差异 …