Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate md5 hash in Angular 5

Tags:

angular

md5

In angular, I want to generate a MD5 hash, so I tried installing npm modules. I tried 'crypto-js', 'angular-md5', but a lot of them with no results.

Finally, I tried with 'ts-md5' and is throwing me an error, as all the others.

First npm install --save ts-md5

and then:

import {Md5} from 'ts-md5/dist/md5';
@Injectable()
export class HeroService {
    constructor(
        private MD5: Md5
    ) { }
}

But I get the error:

[Error] ERROR – Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[HeroService -> Md5]: 
  StaticInjectorError(Platform: core)[HeroService -> Md5]: 
    NullInjectorError: No provider for Md5!
get
resolveToken
tryResolveToken
[...]

Is there a better module to encrypt in md5? How can I solve this error?

like image 448
Alex Avatar asked Mar 24 '18 23:03

Alex


1 Answers

Finally the best way to do it is importing it directly in the component where I have to use it and call it with new Md5().

import {Md5} from 'ts-md5/dist/md5';

const md5 = new Md5();
console.log(md5.appendStr('hello').end());

Thanks to all for the quick responses.

like image 191
Alex Avatar answered Oct 02 '22 22:10

Alex