锡兰的加密哈希 [英] Cryptographic hashing in Ceylon

查看:85
本文介绍了锡兰的加密哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在锡兰中导入标准加密哈希(消息摘要)库(MD5,SHA1,SHA2,SHA256,SHA3等)的推荐方法是什么?

What is the recommended way to import the standard cryptographic hashing (message digest) libraries (MD5, SHA1, SHA2, SHA256, SHA3, etc.) in Ceylon?

推荐答案

SDK

Github上的Ceylon Crypto (以及3个单独的模块在畜群中 ),但它在自述文件中表示:

There is Ceylon Crypto on Github (and as 3 separate modules in Herd), but it says in the README:


请注意,IANAC(我不是密码学家),所以在某些情况下肯定存在缺陷

Note that IANAC (I am not a cryptologist), so this will surely be flawed in some security relevant way.

请勿在生产中使用,也不要以任何昂贵的方式依赖它!

如果您只想在JVM中使用它,我建议仅使用Java的加密API java.security(对于哈希函数应该足够)或javax.crypto(对于诸如密码之类的其他东西)中的s。

If you just want to use this in the JVM, I would suggest to use just Java's crypto APIs in java.security (that should be enough for the hash functions) or javax.crypto (for other stuff like ciphers).

这里是一个示例,计算SHA -256 of Hello World

Here is an example, calculating the SHA-256 of Hello World:

module.ceylon:

module.ceylon:

native("jvm")
module example "1.0.0" {
    import java.base "8";
}

run.ceylon:

run.ceylon:

import java.security {
    MessageDigest
}
import java.lang {
    JString=String,
    ByteArray
}

"Formats a single byte as a (two character) hexadecimal string."
String formatByte(Byte byte)
        => Integer.format(byte.unsigned, 16).padLeading(2, '0');

"Formats a Java byte array as a hexadecimal string."
String formatByteArray(ByteArray result)
        => String.sum(result.byteArray.map(formatByte));

"Calculates SHA-256('Hello World') and print it."
shared void run() {
    value message = "Hello World";
    value bytes = JString(message).getBytes("UTF-8");

    value dig = MessageDigest.getInstance("SHA-256");
    value result = dig.digest(bytes);

    value formatted = formatByteArray(result);
    print("Result: ``result.array```");
    print("Length: ``result.size``");
    print("Result in hex: ``formatted``");
}

此程序输出以下内容:

Result: { -91, -111, -90, -44, 11, -12, 32, 64, 74, 1, 23, 51, -49, -73, -79, -112, -42, 44, 101, -65, 11, -51, -93, 43, 87, -78, 119, -39, -83, -97, 20, 110 }`
Length: 32
Result in hex: A591A6D40BF420404A011733CFB7B190D62C65BF0BCDA32B57B277D9AD9F146E

我没有为此找到锡兰包装纸,但这会使它变得更好一些。

I didn't find a Ceylon wrapper for this which would make it a bit nicer, though.

这篇关于锡兰的加密哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆