Ich versuche crypto-js mit Ionic zu implementieren 2.Ionic 2 Importbibliotheken
ich folgendes getan:
npm install crypto-js typings install dt~crypto-js --global --save
Mein index.d.ts
jetzt enthält:
/// <reference path="globals/crypto-js/index.d.ts" />
A crypto-js
Ordner wurde unter ./typings/global
Ich versuche dann den folgenden Code:
declare var require: any;
import * as CryptoJS from 'crypto-js';
...
private CryptoJS: any;
constructor() {
this.CryptoJS = require("crypto-js");
}
test() {
alert(this.CryptoJS);
}
Sobald ich Referenz versuchen this.CryptoJS
(d alert(this.CryptoJS)
) die App stürzt ab.
Ich mache etwas falsch in der Art, wie ich die crypto-js
Bibliotheken importieren. Bitte kann jemand beraten?
Dank
UPDATE:
folgende this, ich laufe:
>npm install --save @types/cryptojs npm WARN package.json [email protected] No repository field. npm WARN package.json [email protected] No README data npm WARN package.json [email protected] No license field. @types/[email protected] node_modules\@types\cryptojs
Wie kann ich CryptoJS
im Code importieren?
Dank
UPDATE:
import { Injectable } from "@angular/core";
import { LanguageModel } from './languageModel';
import { LocationModel } from './locationModel';
import { JobModel } from './jobModel';
import 'crypto-js';
@Injectable()
export class PersonModel {
public id: number = null;
public joiningDate: number = null;
public lastAccessDate: number = null;
public userName: string = null;
public password: string = null;
public firstName: string = null;
public lastName: string = null;
public emailAddress: string = null;
public locations: LocationModel[] = [];
public languages: LanguageModel[] = [];
public time: string = null;
public avatar: string = null;
public avatar64: string = null;
//private CryptoJS: any;
private SECERET_KEY: string = 'secret key 123';
public getPasswordEcrypted(): string {
// Decrypt
var bytes = CryptoJS.AES.decrypt(this.password.toString(), this.SECERET_KEY);
var plaintext = bytes.toString(CryptoJS.enc.Utf8);
console.log('getPasswordEcrypted', plaintext);
return plaintext;
}
public setPasswordEncrypted(password: string): void {
// Encrypt
alert(password);
console.log('setPasswordEncrypted', password, CryptoJS);
alert(CryptoJS);
var ciphertext = CryptoJS.AES.encrypt(password, this.SECERET_KEY);
alert(ciphertext);
console.log('setPasswordEncrypted', password, ciphertext);
this.password = ciphertext;
}
}
UPDATE:
zu ändern:
import CryptoJS from 'crypto-js';
ergibt:
UPDATE:
Ausführen dieser:
typings install dt~crypto-js --global --save
führt zu keinem Buildfehler, aber während der Laufzeit, wenn CryptoJS
zugegriffen wird (z.B. console.log(CryptoJS);
), stürzt die App ab.
Dies soll der Weg nach vorne sein: https://www.npmjs.com/package/@types/cryptojs – Richard
Könnten Sie bitte sagen, woher Sie den SECRET_KEY bekommen? private SECERET_KEY: string = 'geheimer Schlüssel 123'; Ist es benutzerdefiniert? –