Key.hx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package sys.ssl;
  2. import sys.ssl.Lib;
  3. @:noDoc
  4. typedef KeyPtr = hl.Abstract<"hl_ssl_pkey">;
  5. @:coreApi
  6. class Key {
  7. private var __k : KeyPtr;
  8. private function new( k : KeyPtr ){
  9. __k = k;
  10. }
  11. public static function loadFile( file : String, ?isPublic : Bool, ?pass : String ) : Key {
  12. var data = sys.io.File.getBytes( file );
  13. var start = data.getString(0,11);
  14. if( start == "-----BEGIN " )
  15. return readPEM( data.toString(), isPublic==true, pass );
  16. else
  17. return readDER( data, isPublic==true );
  18. }
  19. public static function readPEM( data : String, isPublic : Bool, ?pass : String ) : Key {
  20. return new Key( key_from_pem( @:privateAccess data.toUtf8(), isPublic, pass == null ? null : @:privateAccess pass.toUtf8() ) );
  21. }
  22. public static function readDER( data : haxe.io.Bytes, isPublic : Bool ) : Key {
  23. return new Key( key_from_der( @:privateAccess data.b, @:privateAccess data.length, isPublic ) );
  24. }
  25. @:hlNative("ssl","key_from_pem") static function key_from_pem( data : hl.Bytes, pub : Bool, pass : Null<hl.Bytes> ) : KeyPtr { return null; }
  26. @:hlNative("ssl","key_from_der") static function key_from_der( data : hl.Bytes, len : Int, pub : Bool ) : KeyPtr { return null; }
  27. }