Skip to content

Instantly share code, notes, and snippets.

@songlairui
Created November 29, 2019 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save songlairui/1821f8580091c2148f45189d16b48444 to your computer and use it in GitHub Desktop.
Save songlairui/1821f8580091c2148f45189d16b48444 to your computer and use it in GitHub Desktop.
generate self signed cerifcate. chrome, android mobile, traefik 生成自签名证书, 可用于 chrome 浏览器,安卓手机,traefik本地证书
var fs = require("fs");
var selfsigned = require("selfsigned");
const SITE_PATTERN = "*.local.dev";
var attrs = [
{ name: "commonName", value: SITE_PATTERN },
{ name: "countryName", value: "cn" },
{ name: "localityName", value: "sz" },
{ name: "stateOrProvinceName", value: "gd" },
{ name: "organizationName", value: "x.y.f.g.z" },
{ name: "organizationalUnitName", value: "ooooo" }
// 添加 emailAddress, 会导致 traefik 使用无效
// { name: "emailAddress", value: "xxx@ccc.com" }
];
var opts = {
days: 3650,
keySize: 2048,
algorithm: "sha256",
// 前两个为默认的 extension 配置
extensions: [
{
name: "basicConstraints",
cA: true // 非 cA, 则 android 无法导入
},
{
name: "keyUsage",
keyCertSign: true,
digitalSignature: true,
nonRepudiation: true,
keyEncipherment: true,
dataEncipherment: true
},
{
name: "subjectAltName",
altNames: [
{
type: 2,
value: SITE_PATTERN
}
]
}
]
};
var pems = selfsigned.generate(attrs, opts);
const { public, private, cert, fingerprint } = pems;
fs.writeFileSync("dev.pub", public);
fs.writeFileSync("dev.key", private);
fs.writeFileSync("dev.crt", cert);
fs.writeFileSync("dev.fingerprint", fingerprint);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment