从磁盘加载证书:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import 'dart:io'; void main() async { var chain = Platform.script.resolve('certificates/server_chain.pem').toFilePath(); var key = Platform.script.resolve('certificates/server_key.pem').toFilePath(); var context = SecurityContext() ..useCertificateChain(chain) ..usePrivateKey(key, password: 'dartdart'); var server = await HttpServer.bindSecure(InternetAddress.anyIPv6, 443, context); await server.forEach((HttpRequest request) { request.response.write('Hello, world!'); request.response.close(); }); } |
从内存加载证书:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import 'dart:io'; void main() async { /// 签名私钥,PEM格式,规范要求:除了开头结尾的 BEGIN / END,以及证书的最后一行外,每行 64个字符,且不能包含空格字符 final String privateKey = ''' -----BEGIN PRIVATE KEY----- ................................................................ -----END PRIVATE KEY----- '''; /// 签名公钥,规范要求:除了开头结尾的 BEGIN / END,以及证书的最后一行外,每行 64个字符,且不能包含空格字符 final String publicKey = ''' -----BEGIN CERTIFICATE----- ................................................................ -----END CERTIFICATE----- '''; var context = SecurityContext() ..useCertificateChainBytes(publicKey.codeUnits) ..usePrivateKeyBytes(privateKey.codeUnits); var server = await HttpServer.bindSecure( InternetAddress.loopbackIPv6, 443, context); await server.forEach((HttpRequest request) { request.response.write('Hello, world!'); request.response.close(); }); } |