如何使用二分搜索比较 x509 证书

发布于 2025-01-04 20:48:52 字数 277 浏览 0 评论 0原文

我有一个列表,该列表有一个 x509certificate,我用它存储在 cms 包中。真是花花公子,直到我遇到了这个问题:我不能让同一个人签名两次。
所以,我知道如何平等地找到该证书,即线性搜索。

这听起来可能纯粹是虚荣心,但我宁愿使用二分搜索,显然使用可比较
我怎么能这么做呢? (javabouncyCastle)。

谢谢

I have a list and that list has an x509certificate, which I use for storing in a cms package. All dandy, until I bump into the problem: I can't have the same person signing twice.
So, I know how to find that certificate by equals, so a linear search.

It may sound pure vanity, but I would rather use a binary search, using, obviously, comparable.
How in heavens could i do that? (java or bouncyCastle).

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如果没结果 2025-01-11 20:48:52

将证书存储在 TreeSet 中,并使用 Collections.binarySearch 查找感兴趣的证书(您必须将 Set 转换为首先列出)。 TreeSet 的底层是一个 TreeMap,因此如果您不喜欢该实现,您可以实现 SortedSet 并创建您自己的。如果不担心重复的证书,请将它们存储在 List 中,并在调用 binarySearch 之前对其进行排序。

您是否有理由不想使用好的 Collection.contains

Store the certs in a TreeSet and use Collections.binarySearch to look for the cert of interest (you'll have to turn your Set into a List first). Underlying a TreeSet is a TreeMap so if you don't like that implementation you can implement SortedSet and make your own. If duplicate certs aren't a concern then store them in a List and sort it before calling binarySearch.

Is there a reason you don't want to use good ol' Collection.contains?

み格子的夏天 2025-01-11 20:48:52

我要感谢 Jensdnault 引导我找到答案:
我将使用(bigInteger)序列号(所有证书都有)对证书进行排序和搜索,以进行比较,一旦找到类似的序列号,我就会在 DER 编码格式。

为什么我没有使用哈希码方法,你可能会问:
并非所有证书和签名都会保存在内存中。有时,有些会位于 PKCS7 (CMS) 或 XMLDSig 文件中。因此,对象的哈希码可能不同,因为物理上它们是不同的对象,但逻辑上它们引用相同的证书。

覆盖 getHashCode 只能给出一半的答案,因为有时序列号可能出现两次的情况并不常见,因为它取决于它所属的链以及其他因素。

感谢您提供的信息和帮助。

I want to thank Jens and dnault for guiding me to the answer:
I am going to sort and search the certificates using the (bigInteger) SerialNumber, which all certificates have, to compare and as soon as I find a similar serial, I am going to compare them in a DER encoded format.

Why did I not used the hash code approach, you may ask:
Not all the certificates and signatures will be in memory. Some, sometimes, will be in a PKCS7 (CMS), or a XMLDSig file. So The object's hashcode may differ, as physically they are different objects, but logically they refer to the same certificate.

Overriding getHashCode will only give half of the answer, as sometimes, not too often the serial number can occur twice, as it depends on the chain that it belongs to, among other factors.

Thank you for the info and the help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文