import { sjcl } from "../vendor/sjcl" ; import { Err , ErrorCode } from "./error" ; import { PBKDF2_ITER_MAX } from "./crypto" ; import { PBES2Container } from "./container" ; /**
 * Interface representing legacy container structure (i.e. encryption format
 * used in Padlock versions 1.x and 2.x)
 */ export interface LegacyContainer { version ? : 1 ; cipher : "aes" ; mode : "ccm" ; iv : string ; adata : string ; ts : 64 | 96 | 128 ; keySize : 256 ; salt : string ; iter : number ; ct : string ; } /**
 * Validates legacy container structure
 */ export function validateLegacyContainer ( obj ) { return ( typeof obj === "object" && ( obj . version === undefined || obj . version === 1 ) && [ 128 , 192 , 256 ] . includes ( obj . keySize ) && typeof obj . iter == "number" && obj . iter <= PBKDF2_ITER_MAX && typeof obj . salt == "string" && [ "aes" ] . includes ( obj . cipher ) && [ "ccm" , "ocb2" ] . includes ( obj . mode ) && typeof obj . iv == "string" && typeof obj . ct == "string" && typeof obj . adata == "string" && [ 64 , 96 , 128 ] . includes ( obj . ts ) ) ; } /**
 * Transforms a legacy container object into an instance of [[PBES2Container]]
 */ export function parseLegacyContainer ( raw ) { if ( ! validateLegacyContainer ( raw ) ) { throw new Err ( ErrorCode . ENCODING_ERROR ) ; } if ( raw . version === undefined ) { raw . adata = sjcl . codec . base64 . fromBits ( raw . adata ) ; } return new PBES2Container ( ) . fromRaw ( { encryptionParams : { algorithm : "AES-CCM" , tagSize : raw . ts , keySize : raw . keySize , iv : raw . iv , additionalData : raw . adata } , keyParams : { algorithm : "PBKDF2" , hash : "SHA-256" , keySize : raw . keySize , iterations : raw . iter , salt : raw . salt } , encryptedData : raw . ct } ) ; }