- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
所以我最近为密码相关方法创建了一个静态类,并且必须创建一个生成安全盐的方法。
最初我实现了 RNGCryptoServiceProvider
并将 n 个字节存入一个数组,我将其转换为 base64 并返回。
问题是输出长度,转换后当然比 n ( which makes sense ) 长。
为了解决这个问题,我将函数更改为下面的方法,我只是想知道通过修剪 base64 字符串是否会引发任何安全风险?
/// <summary>
/// Generates a salt for use with the Hash method.
/// </summary>
/// <param name="length">The length of string to generate.</param>
/// <returns>A cryptographically secure random salt.</returns>
public static string GenerateSalt(int length)
{
// Check the length isn't too short.
if (length < MIN_LENGTH)
{
throw new ArgumentOutOfRangeException("length", "Please increase the salt length to meet the minimum acceptable value of " + MIN_LENGTH + " characters.");
}
// Calculate the number of bytes required.
// https://en.wikipedia.org/wiki/Base64#Padding
// http://stackoverflow.com/questions/17944/how-to-round-up-the-result-of-integer-division
int bytelen = ((3 * length) + 4 - 1) / 4;
// Create our empty salt array.
byte[] bytes = new byte[bytelen];
// Where we'll put our generated salt.
string salt;
// Generate a random secure salt.
using (RNGCryptoServiceProvider randcrypto = new RNGCryptoServiceProvider())
{
// Fill our array with random bytes.
randcrypto.GetBytes(bytes);
// Get a base64 string from the random byte array.
salt = GetBase64(bytes);
}
// Trim the end off only if we need to.
if (salt.Length > length)
{
// Substring is the fastest method to use.
salt = salt.Substring(0, length);
}
// Return the salt.
return salt;
}
还有一个附带问题,我快速浏览了一圈,实际上找不到RNGCryptoServiceProvider
的C#
实现的散列函数到底是什么。有人知道吗?
最佳答案
为什么盐的长度对您如此重要?我不认为有任何真正的安全隐患,因为盐的唯一真正要求是它是随机的和不可猜测的。
换句话说,去争取吧。
编辑:这是使用 Linq 的另一种方法。
Random random = new Random();
int length = 25; // Whatever length you want
char[] keys = "ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890!£$%^&*()".ToCharArray(); // whatever chars you want
var salt = Enumerable
.Range(1, length) // equivalent to the loop bit, for(i.. )
.Select(k => keys[random.Next(0, keys.Length - 1)]) // generate a new random char
.Aggregate("", (e, c) => e + c); // join them together into a string
关于c# - 修剪盐的安全风险,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12068973/
我有一个 Magento 网站,其中似乎没有出现交叉销售产品。 在查看 Stack 和 Google 之后,似乎“重新索引数据”已经为很多人解决了这个问题。 我的问题是,执行此任务是否有任何风险?或者
为了避免在某些简单命令中使用括号,我编写了以下运算符以创建新的图形窗口。我的问题是:除了明显无法在我的变量“ newdev”上执行“ not”功能之外,我是否有“破坏” R中任何内容的风险? # fu
你好 我正在开发一个联系表格。我正在使用邮件功能将其通过电子邮件发送给网站管理员。 是否存在有人可以注入(inject)恶意 javascript 和任何其他注入(inject)攻击的风险? $to
我想知道在 Objective C 中将消息传递给 nil 对象不会执行任何操作,这样依赖是否存在任何风险。 在我的代码中,我有很多对 UIKit 和其他对象的弱引用,这些引用可能随时被清除。由于我来
我被指派修复遗留代码的安全问题,并获得了安全扫描的结果: Poor Error Handling: Server Error Message ( 10932 ) 基本上,当扫描尝试使用一些奇怪的代码进
我有一个匿名类,在创建时需要使用自引用。我的业务代码可以简化为如下代码,我知道这段代码: final Runnable runnable=new Runnable() { @Override
有几个基类是我无法控制的:- class BaseNode // Just a POD class. No VTable { void foo(); } class BaseHost { publ
这个问题已经有答案了: Do I have to guard against SQL injection if I used a dropdown? (11 个回答) 已关闭 7 年前。 我希望我的网
这是根据用户提供的输入创建表的简单过程: PROCEDURE `hackProcedure`( IN tab_name VARCHAR(63)) BEGIN IF (tab_name REGEXP '
我知道在您的网站上显示用户输入时,使用 php 中的 htmlentities() 之类的函数来清理用户输入很有用。但是这样的事情会带来 XSS 风险吗? " /> 像这样清理输入会更好吗? " />
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
谁能阐明图表上曲线的含义、阴影区域的含义以及轴的含义? 最佳答案 这是对风险随时间变化的预测。在 NCrunch wiki 中有一些文档对此进行了描述:http://wiki.ncrunch.net/
我有一个包含 200 万条记录的集合,如果可能的话,我希望在不停机的情况下在其上添加单个字段索引。 我的问题是:是否可以/需要备份集合?使用 azure 门户创建索引在后台运行它而不会造成任何服务停机
我正在使用Symfony2并使用 CSRF token 保护我的表单。 我有一个基于 Ajax 调用的评论系统。如果用户想要编辑他的评论,则会发生以下情况: 用户点击编辑按钮。 通过 ajax 加载“
Checkmark 扫描了我们的代码并显示这些代码存在二阶注入(inject)的风险像这样的代码 @SuppressWarnings("unchecked") public List> findByS
我有一个包含几百行的小型 MySQL 数据库(全部为文本,无图像)。我正在使用 iQuery 请求所有行并在客户端进行所有过滤。 iQuery 代码如下: $(document).ready( fun
如果我的网站只允许用户查看他们自己提交的数据,而永远不允许其他用户提交的数据(即没有一般的“帖子”等)——那么我的网站是否真的存在 XSS 风险? 我仍将致力于 XSS 解决方案(如 httmlspe
我正在构建一个 iframe,而不是 innerHTML , 但带有 createElement .. 我使用了两个不受信任的字符串: iframeEl.title = untrustedStr1;
我正在生成如下动态prepareStatement(字段可能会根据请求输入而变化)。 Veracode 扫描持续报告 SQL 注入(inject)风险 - CWE-89:SQL 命令中使用的特殊元素的
我有一个可以在英语和德语之间切换语言的应用程序。当使用德语时,我希望货币显示将自动转换为德语格式。因此,在我的程序中,我必须检查区域设置,然后根据所选语言转换货币。我选择使用 locale.setDe
我是一名优秀的程序员,十分优秀!