Cannot get expected result for Spring4D cryptography examples

Rick Wheeler

The Spring4D library has cryptography classes, however I cannot get them to work as expected. I'm probably using them incorrectly, however lack of any examples makes it difficult.

For example on the website https://quickhash.com/hash-sha256-online, I can hash the word "test" to generate the following hash:

9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08

Using the Spring4D library, the following code produces a different hash:

CreateSHA256.ComputeHash('test').ToString;

results in:

9EFEA1AEAC9EDA04A892885A65FDAE0E6D9BE8C9FC96DA76D31B929262E12B1D

Upper/lower case aside, it is a different hash altogether. I know must be doing something wrong, but again there's no examples of use so I'm stuck on how to do this.

David Heffernan

Hashing algorithms operate on binary data, typically represented using byte arrays.

Unfortunately, both of the resources you have used offer the ability to hash text. In order to hash text, you first need to convert from text to binary. To do so requires a choice of encoding. And neither method makes it clear what that choice is.

When I use this Delphi code:

LowerCase(CreateSHA256.ComputeHash(TEncoding.UTF8.GetBytes('test')).ToString)

I get the same hash as appears in your question.

I urge you never to attempt to encrypt/hash text and instead regard these operations as operating on binary. Always use an explicit encoding and then encrypt/hash the array of bytes that the encoding produced.

I've picked the UTF-8 encoding here, because it is a full Unicode encoding, and tends to be efficient in terms of space. However, I don't think your online encoder uses UTF-8. In fact I've no idea what encoding it uses, it is unclear on the matter. This is of course the same old issue of text being different from binary.

In my opinion it is a design flaw of the Delphi library that you use that it allows you to hash text without an explicit choice of encoding. If this library must offer a function that hashes text, then it should require the caller to supply an extra TEncoding parameter.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Spring4D中的TForm管理

来自分类Dev

spring4d注销接口类型(spring4d,dunitx,delphi-mocks)

来自分类Dev

我如何在Spring4d中使用THasAttributeFilter

来自分类Dev

无法获得Spring4D加密示例的预期结果

来自分类Dev

如何使用DUnit模拟Spring4D事件

来自分类Dev

编译问题DSharp和Spring4D

来自分类Dev

类型化接口和Spring4D

来自分类Dev

Spring4D TMultiMap即时维护IEnumerable

来自分类Dev

我如何在Spring4d中使用THasAttributeFilter

来自分类Dev

编译问题DSharp和Spring4D

来自分类Dev

spring4d解决本地构造的类

来自分类Dev

如何使用DUnit模拟Spring4D事件

来自分类Dev

Fatal error: Cannot use isset() on the result of an expression

来自分类Dev

How to get the final result of javascript?

来自分类Dev

get_result 返回对象

来自分类Dev

How can i use a rangeByScore get result withing SPRING-DATA-REDIS

来自分类Dev

如何在Spring4D GlobalContainer中初始化主应用程序表单?

来自分类Dev

如何在Spring4d中从ServiceLocator获取子接口实例?

来自分类Dev

使用Spring4D进行构造器注入时“构造器不满意”

来自分类Dev

Spring4d:自动工厂,拥有人:TComponent参数?

来自分类Dev

使用Delphi-Mocks和Spring4D在DUnit中模拟接口

来自分类Dev

如何在Spring4D中使用多接口类

来自分类Dev

Spring4D字段注入在TForm实例中不起作用

来自分类Dev

Spring4D无法对有序字典进行排序

来自分类Dev

使用Delphi-Mocks和Spring4D在DUnit中模拟接口

来自分类Dev

如何在Spring4D中使用多接口类

来自分类Dev

Spring4D 最新版本与 DSharp 不兼容?

来自分类Dev

num_rows:get_result与store_result

来自分类Dev

Get the first occurence of the result in each specified group

Related 相关文章

热门标签

归档