smartssh/ts/smartssh.classes.sshkey.ts

73 lines
2.3 KiB
TypeScript
Raw Normal View History

2016-05-31 17:00:52 +00:00
import "typings-global";
import * as plugins from "./smartssh.plugins";
import * as helpers from "./smartssh.classes.helpers";
2016-04-25 02:06:20 +00:00
export class SshKey {
2016-04-25 02:06:20 +00:00
private privKey:string;
private pubKey:string;
2016-04-26 02:44:50 +00:00
private hostVar:string;
constructor(optionsArg:{private?:string,public?:string,host?:string}={}){
2016-04-25 02:06:20 +00:00
this.privKey = optionsArg.private;
this.pubKey = optionsArg.public;
2016-04-26 02:44:50 +00:00
this.hostVar = optionsArg.host;
2016-04-25 02:06:20 +00:00
};
// getters
2016-04-26 02:44:50 +00:00
get host(){
return this.hostVar;
};
2016-04-25 02:06:20 +00:00
get privateKey(){
return this.privKey;
};
get privateKeyBase64(){
return plugins.base64.encode(this.privKey);
}
get publicKey(){
2016-06-01 02:18:31 +00:00
return this.pubKey;
2016-04-25 02:06:20 +00:00
}
get publicKeyBase64(){
return plugins.base64.encode(this.pubKey);
}
get type(){
if(this.privKey && this.pubKey){
return "duplex";
} else if(this.privKey){
return "private";
} else if(this.pubKey){
return "public";
}
};
// setters
2016-04-26 02:44:50 +00:00
set host(hostArg:string){
this.hostVar = hostArg;
};
2016-04-25 02:06:20 +00:00
set privateKey(privateKeyArg:string){
this.privKey = privateKeyArg;
};
2016-06-13 22:11:20 +00:00
set privateKeyBase64(privateKeyArg:string) {
this.privKey = plugins.base64.decode(privateKeyArg);
}
2016-04-25 02:06:20 +00:00
set publicKey(publicKeyArg:string){
this.pubKey = publicKeyArg;
};
2016-06-13 22:11:20 +00:00
set publicKeyBase64(publicKeyArg:string) {
this.pubKey = plugins.base64.decode(publicKeyArg);
}
2016-04-25 02:06:20 +00:00
store(filePathArg?:string){
let filePathObj = plugins.path.parse(filePathArg);
if(filePathObj.ext = ".priv"){
plugins.smartfile.memory.toFsSync(this.privKey,{fileName:filePathObj.name + filePathObj.ext,filePath:filePathObj.dir});
} else if (filePathObj.ext = ".pub"){
plugins.smartfile.memory.toFsSync(this.pubKey,{fileName:filePathObj.name + filePathObj.ext,filePath:filePathObj.dir});
} else { //we assume we are given a directory as filePathArg, so we store the whole key
plugins.fs.ensureDirSync(filePathObj.dir);
this.store(plugins.path.join(filePathObj.dir,"key.priv")); // call this function recursivly
2016-06-13 22:27:55 +00:00
this.store(plugins.path.join(filePathObj.dir,"key.pub")); // call this function recursivly
2016-04-25 02:06:20 +00:00
}
}
}