我在C#中使用Bouncy Castle库来签署SHA-256,我想在测试自生成证书而不是智能卡读卡器时使用它们.
使用之前使用的自我证书,我有一个加密例外:
指定的算法无效
如果我使用具有相同自我证书的SHA-1签名,那就顺利了.使用智能卡,相同的代码成功运行.
什么是makecert参数?
我最近开始为我的Symfony2项目设置安全性.我选择使用盐对sha256进行编码.当我尝试使用数据库中的示例帐户登录时(使用自我计算的sha256 salt/hash),它在没有给我任何错误消息的情况下仍然失败,我无法弄清楚原因.我决定在Controller的loginAction()方法中添加一些简单的代码.当用户无法使用指定的表单登录时,这是Symfony2调用的方法.我输入了以下代码:
$factory = $this->get('security.encoder_factory');
$em = $this->container->get('doctrine')->getEntityManager();
$userRep = $em->getRepository('MyProjectMyBundle:Users');
$user = $userRep->find(2);
$encoder = $factory->getEncoder($user);
$password = $encoder->encodePassword('cookie', 'thisisasalt');
$user->setPassword($password);
print($password);
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试登录时,Symfony2给了我以下错误:
Catchable Fatal Error: Argument 1 passed to Symfony\Component\Security\Core\Encoder\EncoderFactory::getEncoder() must be an instance of Symfony\Component\Security\Core\User\UserInterface, instance of MyProject\MyBundle\Entity\Users given, called in /var/www/Symfony/src/MyProject/MyBundle/Controller/MainController.php on line 35 and defined in /var/www/Symfony/vendor/symfony/src/Symfony/Component/Security/Core/Encoder/EncoderFactory.php line 33
Run Code Online (Sandbox Code Playgroud)
所以基本上,它说的是getEncoder()必须是一个实例的论证Symfony\Component\Security\Core\User\UserInterface.但是,当我检查MyProject\MyBundle\Entity\Users.php时,它从以下行开始:
<?php
namespace MyProject\MyBundle\Entity;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ORM\Mapping as ORM;
...
Run Code Online (Sandbox Code Playgroud)
所以Users类实际上实现了UserInterface类.它包含UserInterface类中的所有函数.我已经按照Symfony2教程告诉我的方式创建了所有这些文件.Symfony2无法将我的Users实例识别为UserInterface实例的原因是什么?
PS:数据库是由其他人创建的,我只需要使用它.Users表包含的信息不仅仅是UserInterface所需的信息.
正在为我正在进行密码学的自学课程工作(我没有得到这门课程的学分).我需要在大型文件上计算哈希值,其中哈希是逐块完成的.我现在难以理解的是如何将文件分解为这些块?我正在使用python,我很新.
f = open('myfile', 'rb')
BLOCK_SIZE = 1024
m = Crypto.Hash.SHA256.new()
thisHash = ""
blocks = os.path.getsize('myfile') / BLOCK_SIZE #ignore partial last block for now
for i in Range(blocks):
b = f.read(BLOCK_SIZE)
thisHash = m.update(b.encode())
f.seek(block_size, os.SEEK_CUR)
Run Code Online (Sandbox Code Playgroud)
我接近这个吗?代码似乎一直运行直到m.update(b.encode())行执行.我不知道我是否会离开基地或做些什么来完成这项工作.任何建议表示赞赏.谢谢!
(注意:你可能会注意到,这段代码目前并没有真正产生任何东西 - 我只是设置了一些脚手架)
我crypto在服务器端使用NodeJS的捆绑模块进行SHA256散列.在客户端,我使用的是一个名为的JavaScript库Crypto-JS.
我使用SHA256哈希用于使用经典的基于随机数的身份验证的登录系统.但是,即使哈希消息相同,我的服务器端和客户端哈希摘要也不匹配(我已经检查了这一点).甚至散列摘要的长度也不同.
这是我的客户端实现的片段:
var password_hash = CryptoJS.SHA256( token.nonce /*this is the server's nonce*/ + cnonce + password ).toString(CryptoJS.enc.Base64);
Run Code Online (Sandbox Code Playgroud)
这是我的服务器端实现的片段:
var sha256 = CRYPTO.createHash("sha256");
sha256.update(snonce+cnonce+password, "utf-8");
var hash = sha256.digest("base64");
Run Code Online (Sandbox Code Playgroud)
这是一些示例数据:
client-digest: d30ab96e65d09543d7b97d7cad6b6cf65f852f5dd62c256595a7540c3597eec4
server-digest: vZaCi0mCDufqFUwVO40CtKIW7GS4h+XUhTUWxVhu0HQ=
client-message: O1xxQAi2Y7RVHCgXoX8+AmWlftjSfsrA/yFxMaGCi38ZPWbUZBhkVDc5eadCHszzbcOdgdEZ6be+AZBsWst+Zw==b3f23812448e7e8876e35a291d633861713321fe15b18c71f0d54abb899005c9princeofnigeria
server-message: O1xxQAi2Y7RVHCgXoX8+AmWlftjSfsrA/yFxMaGCi38ZPWbUZBhkVDc5eadCHszzbcOdgdEZ6be+AZBsWst+Zw==b3f23812448e7e8876e35a291d633861713321fe15b18c71f0d54abb899005c9princeofnigeria
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么哈希不同?我认为如果它是相同的协议/算法,它将始终产生相同的哈希.
编辑:哇.我去了这个在线哈希工具,它为同一个消息产生了另一个摘要:
4509a6d5028b217585adf41e7d49f0e7c1629c59c29ce98ef7fbb96c6f27502c
Run Code Online (Sandbox Code Playgroud)
编辑编辑:第二个想法,在线哈希工具不同的原因可能是因为它使用了hex我使用的编码base64
我需要生成一些数据的SHA256.我发现这个例子非常好.现在我的问题是我可以使用自己的密钥生成sha256.
编辑:
首先,抱歉错误的问题.我并不是说要更改用于生成SHA256的密钥.我真的需要的是,将以下java代码转换为c ++
public static String calculateHMAC(String data, String key) throws Exception {
String result;
try {
// get an hmac_sha2 key from the raw key bytes
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA2_ALGORITHM);
// get an hmac_sha1 Mac instance and initialize with the signing key
Mac sha256_HMAC = Mac.getInstance(HMAC_SHA2_ALGORITHM);
sha256_HMAC.init(signingKey);
// compute the hmac on input data bytes
byte[] rawHmac = sha256_HMAC.doFinal(data.getBytes());
// base64-encode the hmac
StringBuilder sb = new StringBuilder();
char[] charArray = Base64.encode(rawHmac);
for ( …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过erlang中的sha256加密字符串,但我无法设法获取字符串.crypto:hash(sha256,somestring)给出了一些二进制文件,我怎么能得到字符串?
import hmac
import hashlib
import base64
my = "/api/embedded_dashboard?data=%7B%22dashboard%22%3A7863%2C%22embed%22%3A%22v2%22%2C%22filters%22%3A%5B%7B%22name%22%3A%22Filter1%22%2C%22value%22%3A%22value1%22%7D%2C%7B%22name%22%3A%22Filter2%22%2C%22value%22%3A%221234%22%7D%5D%7D"
key = "e179017a-62b0-4996-8a38-e91aa9f1"
print(hashlib.sha256(my + key).hexdigest())
Run Code Online (Sandbox Code Playgroud)
我得到这个结果:
2df1d58a56198b2a9267a9955c31291cd454bdb3089a7c42f5d439bbacfb3b88
Run Code Online (Sandbox Code Playgroud)
预期结果:
adcb671e8e24572464c31e8f9ffc5f638ab302a0b673f72554d3cff96a692740
Run Code Online (Sandbox Code Playgroud) 本迪戈银行告诉我,我们需要将md5更改为SHA256.我按照他们的指示,我收到了这个错误:
HTTP Status - 400
E5000: Cannot form a matching secure hash based on the merchant's request using either of the two merchant's secrets
Run Code Online (Sandbox Code Playgroud)
他们的示例代码如下:
<?php foreach($_POST as $key => $value) {
if (strlen($value) > 0) { ?>
<input type="hidden" name="<?php echo($key); ?>" value="<?php echo($value); ?>"/><br>
<?php
if ((strlen($value) > 0) && ((substr($key, 0,4)=="vpc_") || (substr($key,0,5) =="user_"))) {
$hashinput .= $key . "=" . $value . "&";
}
}
}
$hashinput = rtrim($hashinput,"&");
?>
<!-- attach SecureHash --> …Run Code Online (Sandbox Code Playgroud) 我不得不实现我自己的HMAC-SHA256用于嵌入式项目.我无法让它工作.我甚至无法得到伪代码,手工计算工作,所以我知道我做错了什么!
我的pseduoCode计算.按照维基百科中的图表
1 function hmac (key, message)
2 if (length(key) > blocksize) then
3 // keys longer than blocksize are shortened
4 key = hash(key)
5 end if
6 if (length(key) < blocksize) then
7 // keys shorter than blocksize are zero-padded
8 key = key ? zeroes(blocksize - length(key))
9 end if
10
11 // Where blocksize is that of the underlying hash function
12 o_key_pad = [0x5c * blocksize] ? key
13 i_key_pad = [0x36 * blocksize] ? …Run Code Online (Sandbox Code Playgroud) 不使用第三方BouncyCastle库,是否可以读取自定义私钥并对消息签名?(使用私钥进行sha256哈希+加密)