Key.hx 978 B

12345678910111213141516171819202122232425262728293031323334
  1. package sys.ssl;
  2. private typedef PKEY = Dynamic;
  3. @:coreApi
  4. class Key {
  5. private var __k : PKEY;
  6. private function new( k : PKEY ){
  7. __k = k;
  8. }
  9. public static function loadFile( file : String, ?isPublic : Bool, ?pass : String ) : Key {
  10. var data = sys.io.File.getBytes( file );
  11. var str = neko.Lib.stringReference(data);
  12. if( str.indexOf("-----BEGIN ") >= 0 )
  13. return readPEM( str, isPublic==true, pass );
  14. else
  15. return readDER( data, isPublic==true );
  16. }
  17. public static function readPEM( data : String, isPublic : Bool, ?pass : String ) : Key {
  18. return new Key( key_from_pem( untyped data.__s, isPublic, pass == null ? null : untyped pass.__s ) );
  19. }
  20. public static function readDER( data : haxe.io.Bytes, isPublic : Bool ) : Key {
  21. return new Key( key_from_der( data.getData(), isPublic ) );
  22. }
  23. private static var key_from_pem = neko.Lib.loadLazy("ssl","key_from_pem",3);
  24. private static var key_from_der = neko.Lib.loadLazy("ssl","key_from_der",2);
  25. }