我一直在试验BCrypt,并发现了以下内容.如果重要的话,我正在运行ruby 1.9.2dev(2010-04-30 trunk 27557)[i686-linux]
require 'bcrypt' # bcrypt-ruby gem, version 2.1.2
@long_string_1 = 'f287ed6548e91475d06688b481ae8612fa060b2d402fdde8f79b7d0181d6a27d8feede46b833ecd9633b10824259ebac13b077efb7c24563fce0000670834215'
@long_string_2 = 'f6ebeea9b99bcae4340670360674482773a12fd5ef5e94c7db0a42800813d2587063b70660294736fded10217d80ce7d3b27c568a1237e2ca1fecbf40be5eab8'
def salted(string)
@long_string_1 + string + @long_string_2
end
encrypted_password = BCrypt::Password.create(salted('password'), :cost => 10)
puts encrypted_password #=> $2a$10$kNMF/ku6VEAfLFEZKJ.ZC.zcMYUzvOQ6Dzi6ZX1UIVPUh5zr53yEu
password = BCrypt::Password.new(encrypted_password)
puts password.is_password?(salted('password')) #=> true
puts password.is_password?(salted('passward')) #=> true
puts password.is_password?(salted('75747373')) #=> true
puts password.is_password?(salted('passwor')) #=> false
Run Code Online (Sandbox Code Playgroud)
起初我认为一旦密码达到一定长度,所有散列中的差异就会丢失,只有当它们非常不相似(即不同的长度)时才会被识别为不同.从我所知的散列函数来看,这对我来说似乎不太合理,但我没有看到更好的解释.
然后,我尝试缩短每个long_strings以查看BCrypt开始能够区分它们的位置,并且我发现如果我将每个长字符串缩短到100个字符左右,最后的尝试('passwor')将开始也是如此.所以现在我不知道该怎么想.
对此有何解释?
我正在玩QFtp(是的......我知道)并且一切正常.
使用他们自己的示例中的代码作为指导.
http://doc.qt.io/archives/qt-4.7/network-qftp-ftpwindow-cpp.html
我遇到的唯一问题是发送(或接收)大文件(比方说3 GB)时,进度条会出现故障.
这是由于从qint64到int的强制转换:
void FtpWindow::updateDataTransferProgress(qint64 readBytes,
qint64 totalBytes)
{
progressDialog->setMaximum(totalBytes);
progressDialog->setValue(readBytes);
}
Run Code Online (Sandbox Code Playgroud)
我想知道在谷歌搜索大约一个小时之后处理这个问题最好的办法是什么,并通过确保我不会超出范围来确保它"安全".
while (totalBytes > 4294967295UL)
{
totalBytes = totalBytes/4294967295UL;
readBytes = readBytes/4294967295UL;
}
Run Code Online (Sandbox Code Playgroud)
但这并不"感觉"正确..
我听说,当计算平均值时,start +(end-start)/ 2与(start + end)/ 2不同,因为后者会导致溢出.我不太明白为什么第二个会导致溢出而第一个不会导致溢出.实现可避免溢出的数学公式的通用规则是什么?
for (int i = 0; i <= 25; i++)
System.out.printf("%d! = %,d\n", i, factorial(i));
Run Code Online (Sandbox Code Playgroud)
上面的代码初始化了下面的阶乘方法:
public static long factorial(int num1)
{
if (num1 == 0)
return 1;
else
return Math.abs(num1 * factorial(num1 - 1));
}
Run Code Online (Sandbox Code Playgroud)
因此,创建了以下输出:
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5,040
8! = 40,320
9! = 362,880
10! = 3,628,800
11! = 39,916,800
12! = 479,001,600
13! = 6,227,020,800
14! = …Run Code Online (Sandbox Code Playgroud) 假设我有一个char指针:
char * cp;
int val2 = 2;
cp = &val2;
*cp = 1234;
Run Code Online (Sandbox Code Playgroud)
由于值1234太大而无法容纳1个字节会发生什么?它会溢出并导致存储不正确的值,还是会以某种方式将正确的值存储在几个字节中?
据我所知,在C++中,当我转换float/ double成int,由此浮点数超出该范围内int可容纳,则结果没有被定义为C++语言的一部分.结果取决于实现/编译器.常见的编译器使用什么策略来处理这个问题?
转换7.2E12为a int可以产生值1634811904或2147483647.例如,有没有人知道编译器在每种情况下做了什么?
c++ compiler-construction integer-overflow type-conversion floating-point-conversion
我有以下C代码:
uint8_t firstValue = 111;
uint8_t secondValue = 145;
uint16_t temp = firstValue + secondValue;
if (temp > 0xFF) {
return true;
}
return false;
Run Code Online (Sandbox Code Playgroud)
这是替代实现:
uint8_t firstValue = 111;
uint8_t secondValue = 145;
if (firstValue + secondValue > 0xFF) {
return true;
}
return false;
Run Code Online (Sandbox Code Playgroud)
第一个例子是显而易见的,uint16_t类型足够大以包含结果.当我clang在OS/X上使用编译器尝试第二个示例时,它正确返回true.那里发生了什么?是否有某种临时的,更大的类型来包含结果?
我已经编写了此C代码,用于查找等于其数字的阶乘之和的所有整数的总和。在没有任何GCC优化标志的情况下完成该工作大约需要一分钟,使用-O1可以将时间减少约15-20秒,但是当我尝试使用-O2,-O3或-Os时,它陷入了无限循环。
int main()
{
int i, j, factorials[10];
int Result=0;
for(i=0; i<10; i++)
{
factorials[i]=1;
for(j=i; j>0; j--)
{
factorials[i] *= j;
}
}
for(i=3; i>2; i++) //This is the loop the program gets stuck on
{
int Sum=0, number=i;
while(number)
{
Sum += factorials[number % 10];
number /= 10;
}
if(Sum == i)
Result += Sum;
}
printf("%d\n", Result);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我已查明这for(i=3; i>2; i++)是问题的原因。所以很明显i永远不会小于2?
这与整数溢出行为未定义有关吗?如果是这样,那么有关这些情况下程序究竟发生了什么的更多信息?
编辑:我想我应该提到,我知道其他编写for循环的方法,这样它就不会使用溢出(我希望INT_MAX + 1等于<2的INT_MIN),但这是只是做一个随机测试以查看会发生什么,所以我在这里张贴了它以了解到底发生了什么:)
我有一个遗留的代码库,我们正试图从中迁移devtoolset-4到devtoolset-7.我注意到有关有符号整数溢出的有趣行为(int64_t具体而言).
有一个代码片段,用于检测整数溢出,同时乘以一大组整数:
// a and b are int64_t
int64_t product = a * b;
if (b != 0 && product / b != a) {
// Overflow
}
Run Code Online (Sandbox Code Playgroud)
这段代码与devtoolset-4一起工作正常.但是,使用devtoolset-7时,永远不会检测到溢出.
例如:当a = 83802282034166和b = 98765432,
product变为-5819501405344925872(显然值已溢出).
但product / b结果价值等于a (83802282034166).因此,if情况永远不会成真.它的值应该根据overflown(负)product值计算:-5819501405344925872 / 98765432 = -58922451788
具有讽刺意味的是,数学是正确的,但它导致devtoolset-4的异常行为.
product / b != a
为product != a * b并达到相同的溢出值(或者可能只是根据上面的语句跳过计算 …我有一个高达2 000 000的素数列表.这是一个包含近15万个非常大的整数的列表.我想要一个所有数字的总和.这是一个随机的大整数列表,仅用于演示:
List<int> numbers = new List<int>();
for (int i = 0; i < 100; i++)
{
numbers.Add(1000000000);
}
Console.WriteLine(numbers.Sum().ToString());
Run Code Online (Sandbox Code Playgroud)
我得到一个"算术运算导致溢出"异常.我猜这个总和太大了,但将它转换为Int64并没有帮助,它仍然会抛出相同的异常.
Console.WriteLine(Convert.ToUInt64(numbers.Sum()).ToString());
Run Code Online (Sandbox Code Playgroud)
我甚至尝试将总和保存到Int64变量然后使用它,但这也不起作用.
long sum = numbers.Sum();
Console.WriteLine(sum.ToString());
Run Code Online (Sandbox Code Playgroud)
是否有任何数据类型可以容纳这么大的数字,或者我是否在其他地方犯了错误?谢谢你的帮助.
integer-overflow ×10
c++ ×4
c ×3
.net ×1
algorithm ×1
bcrypt ×1
c# ×1
char ×1
devtoolset ×1
encryption ×1
factorial ×1
gcc ×1
java ×1
linq ×1
long-integer ×1
math ×1
optimization ×1
passwords ×1
qt ×1
qt4 ×1
ruby ×1