如何用C#代码替代openSSL调用?

0 投票
1 回答
704 浏览
提问于 2025-04-15 20:55

今天我在为Chrome制作一个新的主题创建工具时遇到了问题。你可能知道,Chrome使用一种叫做CRX的新文件格式来管理它的插件和主题。这种格式其实是一个基本的压缩文件,只不过稍微改了一下:

“Cr24” + derkey + 签名 + zip文件

问题来了。目前只有两个CRX创建工具,分别是用Ruby和Python写的。我对这两种语言都不太了解(虽然我对Python有一点基础,但主要是用在PyS60上),所以我想请你帮我把这个Python应用转换成不依赖外部程序的C#代码。

另外,这里是crxmake.py的源代码:



#!/usr/bin/python
# Cribbed from http://github.com/Constellation/crxmake/blob/master/lib/crxmake.rb
# and http://src.chromium.org/viewvc/chrome/trunk/src/chrome/tools/extensions/chromium_extension.py?revision=14872&content-type=text/plain&pathrev=14872

# from: http://grack.com/blog/2009/11/09/packing-chrome-extensions-in-python/
import sys
from array import *
from subprocess import *
import os
import tempfile

def main(argv):

  arg0,dir,key,output = argv

  # zip up the directory

  input = dir + ".zip"

  if not os.path.exists(input):
    os.system("cd %(dir)s; zip  -r ../%(input)s . -x '.svn/*'" % locals())
  else:
    print "'%s' already exists using it" % input

  # Sign the zip file with the private key in PEM format
  signature = Popen(["openssl", "sha1", "-sign", key, input], stdout=PIPE).stdout.read();

  # Convert the PEM key to DER (and extract the public form) for inclusion in the CRX header
  derkey = Popen(["openssl", "rsa", "-pubout", "-inform", "PEM", "-outform", "DER", "-in", key], stdout=PIPE).stdout.read();

  out=open(output, "wb");
  out.write("Cr24")  # Extension file magic number
  header = array("l");
  header.append(2); # Version 2
  header.append(len(derkey));
  header.append(len(signature));
  header.tofile(out);
  out.write(derkey)
  out.write(signature)
  out.write(open(input).read())

  os.unlink(input)
  print "Done."

if __name__ == '__main__':
  main(sys.argv)

你能帮我吗?

1 个回答

0

对于Linux,有很多工具可以做到这一点,包括一个适用于bash的工具,但听起来你想要的是Windows上的工具(我猜是因为你提到了C#)。

我本来想把所有工具的链接都放上来,但我还是新手,只能发一个链接。

不过,这些工具都可以在Windows上使用,但需要先设置语言解释器和OpenSSL。所以我花了几个小时,做了一个可以在Windows和Linux上运行的C语言版本(不过,我不太明白为什么你会在Linux上使用它)。

这个版本已经把OpenSSL静态链接了,所以不需要额外的解释器或库。

你可以在这里找到这个项目:http://github.com/kylehuff/buildcrx

需要说明的是,我并不使用Windows,这个程序是在Linux上写的,Windows的可执行文件是在Linux上交叉编译的(还有OpenSSL库也是如此)。我在Windows上测试过,但没有深入测试。如果你遇到问题,请不要在这里请求支持;我不会回复。请使用上面链接中的问题页面。

另外,我不会把这个转换成C#,因为我觉得对于这么简单的工具,没有必要使用C#,这样只会让它在其他平台上变得更复杂,需要“其他软件”才能使用。

撰写回答