密码线程安全吗? [英] Is Cipher thread-safe?
问题描述
很简单,javax.crypto.Cipher
的一个实例(例如 Cipher.getInstance("RSA")
)可以从多个线程使用,还是我需要将它们中的多个粘贴到 ThreadLocal
(在我的情况下)?
Quite simply, can one instance of javax.crypto.Cipher
(e.g. Cipher.getInstance("RSA")
) be used from multiple threads, or do I need to stick multiple of them in a ThreadLocal
(in my case)?
推荐答案
不,不是.实例是有状态的.因此,您需要将其存储在线程本地,或者在每次加密/解密调用时获取一个新实例,或者将其包装在 synchronized(cipher)
块中.
No, it isn't. The instance is stateful. So you need to store it threadlocal, or to obtain a new instance on every encrypt/decrypt call, or to wrap it in a synchronized(cipher)
block.
线程安全在 javadoc 中通常被称为是线程安全的"或 "不是线程安全".Cipher
,所以你不应该认为它是线程安全的.
Threadsafety is usually mentioned in javadocs as "is thread safe" or "is not thread safe". This is not the case for Cipher
, so you should not assume it to be threadsafe.
这篇关于密码线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!