import { b64tohex , hex2b64 } from "./lib/jsbn/base64" ; import { JSEncryptRSAKey } from "./JSEncryptRSAKey" ; import version from './version.json' ; export interface IJSEncryptOptions { default_key_size ? : string ; default_public_exponent ? : string ; log ? : boolean ; } param { Object } options constructor export class JSEncrypt { constructor ( options = { } ) { options = options || { } ; this . default_key_size = options . default_key_size ? parseInt ( options . default_key_size , 10 ) : 1024 ; this . default_public_exponent = options . default_public_exponent || "010001" ; this . log = options . log || false ; this . key = null ; } private default_key_size : number ; private default_public_exponent : string ; private log : boolean ; private key : JSEncryptRSAKey ; public static version : string = version . version ; param { Object | string } key public public setKey ( key ) { if ( this . log && this . key ) { console . warn ( "A key was already set, overriding existing." ) ; } this . key = new JSEncryptRSAKey ( key ) ; } see setKey public public setPrivateKey ( privkey ) { this . setKey ( privkey ) ; } see setKey public public setPublicKey ( pubkey ) { this . setKey ( pubkey ) ; } param { string } str return { string } public public decrypt ( str ) { try { return this . getKey ( ) . decrypt ( b64tohex ( str ) ) ; } catch ( ex ) { return false ; } } param { string } str return { string } public public encrypt ( str ) { try { return hex2b64 ( this . getKey ( ) . encrypt ( str ) ) ; } catch ( ex ) { return false ; } } public encryptLong ( str ) { try { return hex2b64 ( this . getKey ( ) . encryptLong ( str ) ) ; } catch ( ex ) { return false ; } } public decryptLong ( str ) { try { return this . getKey ( ) . decryptLong ( b64tohex ( str ) ) ; } catch ( ex ) { return false ; } } param { string } str param { function } digestMethod param { string } digestName return { string } public public sign ( str , digestMethod , digestName ) { try { return hex2b64 ( this . getKey ( ) . sign ( str , digestMethod , digestName ) ) ; } catch ( ex ) { return false ; } } param { string } str param { string } signature param { function } digestMethod return { boolean } public public verify ( str , signature , digestMethod ) { try { return this . getKey ( ) . verify ( str , b64tohex ( signature ) , digestMethod ) ; } catch ( ex ) { return false ; } } param { callback } cb returns { JSEncryptRSAKey } public public getKey ( cb ? ) { if ( ! this . key ) { this . key = new JSEncryptRSAKey ( ) ; if ( cb && { } . toString . call ( cb ) === "[object Function]" ) { this . key . generateAsync ( this . default_key_size , this . default_public_exponent , cb ) ; return ; } this . key . generate ( this . default_key_size , this . default_public_exponent ) ; } return this . key ; } returns { string } public public getPrivateKey ( ) { return this . getKey ( ) . getPrivateKey ( ) ; } returns { string } public public getPrivateKeyB64 ( ) { return this . getKey ( ) . getPrivateBaseKeyB64 ( ) ; } returns { string } public public getPublicKey ( ) { return this . getKey ( ) . getPublicKey ( ) ; } returns { string } public public getPublicKeyB64 ( ) { return this . getKey ( ) . getPublicBaseKeyB64 ( ) ; } }