在Swift应用程序中使用哈希密码 [英] Hash password in Swift application

查看:235
本文介绍了在Swift应用程序中使用哈希密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了安全起见,我将加密一些数据,包括我的应用程序中的用户密码。



我的同事选择了scrypt哈希算法,长度为64字节,一个固定的种子,然后转换为十六进制。

HashingA12345678Z导致: 25fac84a1cc3a8f6706848d1016cfe7e9d3631691306dcacae68c11c7b54f0bf89e7a7fc51f7fcc19671775acb21c8d928c4c96bb66d915925de58b8b36ab251



种子是 HeanpyftAkWilfUd



在服务器上,使用此实现: https://github.com/ricmoo/pyscrypt



示例:

  scrypt.hash(A12345678Z,HeanpyftAkWilfUd)。encode('hex ')

- >

  25fac84a1cc3a8f6706848d1016cfe7e9d3631691306dcacae68c11c7b54f0bf89e7a7fc51f7fcc19671775acb21c8d928c4c96bb66d915925de58b8b36ab251 

我的问题是如何在Swift中做到这一点?使用什么库,如果可能的话 - 向我展示示例代码,这将导致哈希 A12345678Z 到这个:

  25fac84a1cc3a8f6706848d1016cfe7e9d3631691306dcacae68c11c7b54f0bf89e7a7fc51f7fcc19671775acb21c8d928c4c96bb66d915925de58b8b36ab251 


解决方案

a href =https://github.com/jedisct1/swift-sodium =nofollow> Swift-Sodium 。它是钠加密库的Swift界面。



以下是一个来自README.md的例子

  let sodium = Sodium()! 
让密码=正确的马电池装订.toData()!
let hashedStr = sodium.pwHash.scrypt.str(密码,
opsLimit:sodium.pwHash.scrypt.OpsLimitInteractive,
memLimit:sodium.pwHash.scrypt.MemLimitInteractive)!

如果sodium.pwHash.scrypt.strVerify(hashStr,passwd:password)== false {
//密码不匹配给定的散列字符串
}


For security purposes I will encrypt some data, including the user password in my application.

My colleagues have chosen scrypt hashing algorithm, for a 64 bytes length, with a fixed seed, then converted to hex.

Hashing "A12345678Z" leads to: 25fac84a1cc3a8f6706848d1016cfe7e9d3631691306dcacae68c11c7b54f0bf89e7a7fc51f7fcc19671775acb21c8d928c4c96bb66d915925de58b8b36ab251

Seed is "HeanpyftAkWilfUd".

On server, they are using this implementation : https://github.com/ricmoo/pyscrypt

Example:

scrypt.hash("A12345678Z", "HeanpyftAkWilfUd").encode('hex’)

->

25fac84a1cc3a8f6706848d1016cfe7e9d3631691306dcacae68c11c7b54f0bf89e7a7fc51f7fcc19671775acb21c8d928c4c96bb66d915925de58b8b36ab251

My question is how to do that in Swift? What library to use and if it possible - show me sample code, that will lead hashing "A12345678Z" to exactly this:

25fac84a1cc3a8f6706848d1016cfe7e9d3631691306dcacae68c11c7b54f0bf89e7a7fc51f7fcc19671775acb21c8d928c4c96bb66d915925de58b8b36ab251

解决方案

You could use Swift-Sodium. It's a Swift interface for the Sodium crypto library.

Here's an example from the README.md

let sodium = Sodium()!
let password = "Correct Horse Battery Staple".toData()!
let hashedStr = sodium.pwHash.scrypt.str(password,
opsLimit: sodium.pwHash.scrypt.OpsLimitInteractive,
memLimit: sodium.pwHash.scrypt.MemLimitInteractive)!

if sodium.pwHash.scrypt.strVerify(hashStr, passwd: password) == false {
   // Password doesn't match the given hash string
}

这篇关于在Swift应用程序中使用哈希密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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