GithubHelp home page GithubHelp logo

robthree / nidenticon Goto Github PK

View Code? Open in Web Editor NEW
94.0 8.0 12.0 604 KB

NIdenticon is a library for creating simple Identicons

License: MIT License

C# 96.05% CSS 0.28% HTML 3.46% ASP.NET 0.22%
c-sharp dotnet identicon avatar-generator

nidenticon's Introduction

logo NIdenticon

"An Identicon is a visual representation of a hash value, usually of an IP address, that serves to identify a user of a computer system as a form of avatar while protecting the users' privacy. The original Identicon was a 9-block graphic, and the representation has been extended to other graphic forms by third parties." – Wikipedia

NIdenticon is a library (available as NuGet package) that helps creating simple Identicons but is flexible enough to allow for tweaking the output to your heart's content.

This project was inspired by:

Usage

The most basic example is as follows:

var g = new IdenticonGenerator();
var mybitmap = g.Create("foo");

This will create an Identicon based on the string foo with all other options default; another example is:

var g = new IdenticonGenerator();
var mybitmap = g.Create(HttpContext.Current.Request.UserHostAddress);

This creates an Identicon based on the IP-address of a remote host. The Identicon can be created with the following options:

  • Dimensions (width, height default: 400, 400)
  • Number of blocks, or "pixels" (horizontal, vertical default: 6, 6)
  • Hash algorithm (default: SHA512)
  • Background color (default: Transparent)
  • Even the default string-encoding used can be specified (default: UTF-8)

This library only contains one the IdenticonGenerator that has only one method: Create(), but which is rich in overloads. These are:

Bitmap Create(byte[] value)
Bitmap Create(byte[] value, Size size)
Bitmap Create(byte[] value, Size size, Color backgroundcolor)
Bitmap Create(byte[] value, Size size, Color backgroundcolor, Size blocks)
Bitmap Create(byte[] value, Size size, Color backgroundcolor, Size blocks, IBlockGenerator[] blockgenerators)
Bitmap Create(byte[] value, Size size, Color backgroundcolor, Size blocks, IBlockGenerator[] blockgenerators, IBrushGenerator brushgenerator)
Bitmap Create(byte[] value, Size size, Color backgroundcolor, Size blocks, IBlockGenerator[] blockgenerators, IBrushGenerator brushgenerator, string algorithm)
Bitmap Create(IPAddress ipaddress)
Bitmap Create(IPAddress ipaddress, Size size)
Bitmap Create(IPAddress ipaddress, Size size, Color backgroundcolor)
Bitmap Create(IPAddress ipaddress, Size size, Color backgroundcolor, Size blocks)
Bitmap Create(IPAddress ipaddress, Size size, Color backgroundcolor, Size blocks, IBlockGenerator[] blockgenerators)
Bitmap Create(IPAddress ipaddress, Size size, Color backgroundcolor, Size blocks, IBlockGenerator[] blockgenerators, IBrushGenerator brushgenerator)
Bitmap Create(IPAddress ipaddress, Size size, Color backgroundcolor, Size blocks, IBlockGenerator[] blockgenerators, IBrushGenerator brushgenerator, string algorithm)
Bitmap Create(string value)
Bitmap Create(string value, Size size)
Bitmap Create(string value, Size size, Color backgroundcolor)
Bitmap Create(string value, Size size, Color backgroundcolor, Size blocks)
Bitmap Create(string value, Size size, Color backgroundcolor, Size blocks, Encoding encoding)
Bitmap Create(string value, Size size, Color backgroundcolor, Size blocks, Encoding encoding, IBlockGenerator[] blockgenerators)
Bitmap Create(string value, Size size, Color backgroundcolor, Size blocks, Encoding encoding, IBlockGenerator[] blockgenerators, IBrushGenerator brushgenerator)
Bitmap Create(string value, Size size, Color backgroundcolor, Size blocks, Encoding encoding, IBlockGenerator[] blockgenerators, IBrushGenerator brushgenerator, string algorithm)

Parameters not specified will resort to the DefaultXXX-properties which can be specified when instantiating an IdenticonGenerator; the IdenticonGenerator's constructor is also rich in overloads:

public IdenticonGenerator()
public IdenticonGenerator(string algorithm)
public IdenticonGenerator(string algorithm, Size size)
public IdenticonGenerator(string algorithm, Size size, Color defaultBackgroundColor)
public IdenticonGenerator(string algorithm, Size size, Color defaultBackgroundColor, Size defaultBlocks)
public IdenticonGenerator(string algorithm, Size size, Color defaultBackgroundColor, Size defaultBlocks, Encoding encoding)
public IdenticonGenerator(string algorithm, Size size, Color defaultBackgroundColor, Size defaultBlocks, Encoding encoding, IBlockGenerator[] blockgenerators)
public IdenticonGenerator(string algorithm, Size size, Color defaultBackgroundColor, Size defaultBlocks, Encoding encoding, IBlockGenerator[] blockgenerators, IBrushGenerator brushgenerator)

(Ofcourse, the DefaultXXX properties can be changed after instantiating an IdenticonGenerator as well)

This allows you to specify defaults only once (using the ctor) or for each generated Identicon (passing parameters to the Create() methods. Another approach is to use the fluent syntax to set only the desired defaults without having to specify all other parameters in the ctor:

var g = new IdenticonGenerator()
        .WithSize(96,96)
        .WithBlocks(6,6)
        .WithBlockGenerators(IdenticonGenerator.ExtendedBlockGeneratorsConfig);

Adhering to the SOLID principle, it is possible to create your own "BlockGenerators" and "BrushGenerators" by simply implementing their interfaces IBlockGenerator and IBrushGenerator. This way you can influence the shapes and colors of the Identicon. There are a few defaults provided as static members of the IdenticonGenerator class; these are:

  • DefaultBlockGeneratorsConfig which only draws rectangles
  • ExtendedBlockGeneratorsConfig which draws different kinds of shapes (triangles, pie-parts, rectangles and rotated rectangles)
  • DefaultBrushGeneratorConfig which chooses a "random" (based on the input value to make the process deterministic) color.

Some BlockGenerators and BrushGenerators are provided; the rest is up to you. The blockgenerators are associated with a weight so that you can influence the probability of a blockgenerator to be chosen when the IdenticonGenerator is selecting a blockgenerator for a specific block in the Identicon.

#Notes

  1. Do note that the Create() method and its overloads all return a Bitmap object; you have to take care of storing it, sending it to the browser or whatever you need to do. Also note that you might want to Dispose() the returned Identicon when no longer needed.

  2. Also note that NIdenticon will round the image-dimensions DOWN to the nearest available size when the dimensions aren't exactly divisible by the horizontal/vertical blocks. Dimensions where width/horizontalblocks and height/verticalblocks are a multiple of oneanother work best (for example: for a width of 400 px you can use 2, 4, and 8 horizontal blocks but 6 will result in an image with a width of 396).

  3. You might want to 'salt' your values (per Identicon (e.g. 'user' or 'account' for example) or per application, domain etc.) to make the Identicons even harder to 'reverse engineer'. A simple myIDGenerator.Create(user.Email + user.Salt) for example will do.

Example

A simple Windows Forms application and sample ASP.Net MVC website is provided for you so you can explore the options and quickly see what the result will look like.

Below are some example images:

Result Algorithm Value Blockgens Background Blocks Brush
MD5 Identicon Default White 6x6 Static
MD5 Identicon Extended White 6x6 Static
SHA256 Identicon Extended White 6x6 Static
SHA512 Identicon Extended White 6x6 Static
MD5 RobIII Extended Transparent 6x6 Random
RipeMD160 RobIII Extended Transparent 6x6 Random
SHA1 RobIII Extended Transparent 6x6 Random
SHA256 RobIII Extended Transparent 6x6 Random
SHA384 RobIII Extended Transparent 6x6 Random
SHA512 RobIII Extended Transparent 6x6 Random
SHA256 192.168.1.1 Extended Transparent 6x6 Static
SHA256 192.168.1.2 Extended Transparent 6x6 Static
SHA256 192.168.1.1 Extended Black 6x6 Static
SHA256 192.168.1.2 Extended Black 6x6 Static
SHA1 Foobar Extended Black 6x6 Static
SHA1 FooBar Extended Black 6x6 Static
MD5 foo_bar Extended Transparent 4x4 Static
MD5 foo_bar Extended Transparent 6x6 Static
MD5 foo_bar Extended Transparent 12x12 Static
MD5 foo_bar Default Transparent 4x6 Static
MD5 foo_bar Default Transparent 6x4 Static
SHA256 Identicon Extended Transparent 6x6 Random
SHA256 Identicon Extended Transparent 6x6 Static
SHA384 Identicon Custom
(Triangles only)
Transparent 6x6 Random
SHA384 Identicon Custom
(Pie-slices only)
Transparent 6x6 Random
SHA384 Identicon Custom
(Rectangle + Pie in 4:1)
Transparent 6x6 Static

Assembly Strong Naming & Usage in Signed Applications

This module produces strong named assemblies when compiled. When consumers of this package require strongly named assemblies, for example when they themselves are signed, the outputs should work as-is. The key file to create the strong name is adjacent to the csproj file in the root of the source project. Please note that this does not increase security or provide tamper-proof binaries, as the key is available in the source code per Microsoft guidelines

Build status NuGet version

nidenticon's People

Contributors

georgdangl avatar robthree avatar tomasherceg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nidenticon's Issues

Use in Blazor (wasm)?

Does someone know how to use this in blazor wasm?

Ideally, to generate the image and insert it into the DOM, without an intermediate image file.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.