第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何正確調(diào)整 Go 中的 Argon2 參數(shù)以減少內(nèi)存消耗?

如何正確調(diào)整 Go 中的 Argon2 參數(shù)以減少內(nèi)存消耗?

C#
函數(shù)式編程 2022-06-13 17:15:42
Argon2在設計上是很耗內(nèi)存的。在半官方的 Go 實現(xiàn)中,使用時建議使用以下參數(shù)IDKey:key := argon2.IDKey([]byte("some password"), salt, 1, 64*1024, 4, 32)其中1是時間參數(shù),64*1024是內(nèi)存參數(shù)。這意味著庫在散列值時將創(chuàng)建一個 64MB 的緩沖區(qū)。在許多散列過程可能同時運行的情況下,這會對主機內(nèi)存造成很大壓力。在內(nèi)存消耗過多的情況下,建議減少內(nèi)存參數(shù)并增加時間因子:RFC 草案建議[2] time=1,memory=64*1024 是一個合理的數(shù)字。如果在某些情況下無法使用該內(nèi)存量 (64 MB),則可以增加時間參數(shù)以進行補償。所以,假設我想將內(nèi)存消耗限制為 16MB(推薦的 64MB 的 1/4),我仍然不清楚我應該如何調(diào)整time參數(shù):這應該是4 倍,以便內(nèi)存和時間保持不變?或者在時間和記憶的相關性背后還有其他一些邏輯在起作用嗎?
查看完整描述

2 回答

?
慕少森

TA貢獻2019條經(jīng)驗 獲得超9個贊

RFC 草案建議[2] time=1,memory=64*1024 是一個合理的數(shù)字。如果在某些情況下無法使用該內(nèi)存量 (64 MB),則可以增加時間參數(shù)以進行補償。

我認為這里的關鍵是“補償”這個詞,所以在這種情況下,它試圖說:要實現(xiàn)與 相似的哈希復雜性IDKey([]byte("some password"), salt, 1, 64*1024, 4, 32),你可以嘗試IDKey([]byte("some password"), salt, 4, 16*1024, 4, 32)。
但是,如果您想降低散列結(jié)果的復雜性(并降低性能開銷),您可以減小memory uint32忽略time參數(shù)的大小。

這應該是 4 倍,以便內(nèi)存和時間的乘積保持不變?

我不這么認為,我相信memory這里的意思是結(jié)果哈希的長度,但time參數(shù)可能意味著“哈希結(jié)果需要重新哈希多少次,直到我得到最終結(jié)果”。

所以這兩個參數(shù)是相互獨立的。這些只是控制您想要實現(xiàn)多少“由于時間-內(nèi)存權(quán)衡而節(jié)省的蠻力成本”


查看完整回答
反對 回復 2022-06-13
?
寶慕林4294392

TA貢獻2021條經(jīng)驗 獲得超8個贊

難度大致等于time_cost * memory_cost (并且可能/ parallelism)。所以如果你0.25x的內(nèi)存成本,你應該4x時間成本。另請參閱此答案。


// The time parameter specifies the number of passes over the memory and the

// memory parameter specifies the size of the memory in KiB.

查看 Argon2 API 本身。我將稍微交叉引用并使用argon2-cffi 文檔。貌似go接口在底層使用了C-FFI(外來函數(shù)接口) ,所以protoype應該是一樣的。


Parameters

time_cost (int) – Defines the amount of computation realized and therefore the execution time, given in number of iterations.


memory_cost (int) – Defines the memory usage, given in kibibytes.


parallelism (int) – Defines the number of parallel threads (changes the resulting hash value).


hash_len (int) – Length of the hash in bytes.


salt_len (int) – Length of random salt to be generated for each password.


encoding (str) – The Argon2 C library expects bytes. So if hash() or verify() are passed an unicode string, it will be encoded using this encoding.


type (Type) – Argon2 type to use. Only change for interoperability with legacy systems.

事實上,如果我們查看 Go 文檔:


// The draft RFC recommends[2] time=1, and memory=64*1024 is a sensible number.

// If using that amount of memory (64 MB) is not possible in some contexts then

// the time parameter can be increased to compensate.

//

// The time parameter specifies the number of passes over the memory and the

// memory parameter specifies the size of the memory in KiB. For example

// memory=64*1024 sets the memory cost to ~64 MB. The number of threads can be

// adjusted to the numbers of available CPUs. The cost parameters should be

// increased as memory latency and CPU parallelism increases. Remember to get a

// good random salt.

我不是 100% 清楚線程數(shù)的影響,但我相信它確實并行化了散列,并且像任何多線程作業(yè)一樣,這減少了大約1/NN 個內(nèi)核所花費的總時間。顯然,您應該將并行度設置為 cpu count。


查看完整回答
反對 回復 2022-06-13
  • 2 回答
  • 0 關注
  • 424 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號