AnkiIO 1.0.2
Build, validate, import, and export Anki-compatible decks from .NET
Loading...
Searching...
No Matches
AnkiId.cs
1using System.Buffers.Binary;
2using System.Security.Cryptography;
3using System.Text;
4
5namespace AnkiIO;
6
14public static class AnkiId
15{
16 private static long last = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() * 1_000;
17
31 public static long New() => Interlocked.Increment(ref last);
32
57 public static long FromStableValue(string scope, string value)
58 {
59 ArgumentException.ThrowIfNullOrWhiteSpace(scope);
60 ArgumentException.ThrowIfNullOrWhiteSpace(value);
61 var bytes = SHA256.HashData(Encoding.UTF8.GetBytes(scope + "\0" + value));
62 return (BinaryPrimitives.ReadInt64LittleEndian(bytes) & long.MaxValue) is var result && result != 0 ? result : 1;
63 }
64}
Creates positive 64-bit identifiers for new objects or repeatable external imports.
Definition AnkiId.cs:15
static long FromStableValue(string scope, string value)
Derives a deterministic positive identifier from a caller-controlled namespace and stable value.
Definition AnkiId.cs:57
static long New()
Creates a positive, process-unique identifier for a new deck, note type, note, or card.
Definition AnkiId.cs:31