public void TestRotatePublicServerKey() { var phe = new PheCrypto(); var oldPublicServerKey = phe.DecodePublicKey(this.serverPublic); var newPublicServerKey = phe.RotatePublicKey(oldPublicServerKey, this.token); Assert.Equal(this.rotatedServerPub, newPublicServerKey.Encode()); }
public static ProtocolContext Create(string appId, string accessToken, string serverPublicKey, string clientSecretKey, string[] updateTokens = null) { var phe = new PheCrypto(); var(pkSVer, pkS) = EnsureServerPublicKey(serverPublicKey, phe); var(skCVer, skC) = EnsureClientSecretKey(clientSecretKey, phe); if (pkSVer != skCVer) { throw new ArgumentException("Incorrect versions for Server/Client keys."); } var serializer = new HttpBodySerializer(); var client = new PheClient(serializer) { AccessToken = accessToken, BaseUri = new Uri("https://api.passw0rd.io/") }; var ctx = new ProtocolContext { AppId = appId, Client = client, Crypto = phe }; var serverPksDictionary = new Dictionary <int, PublicKey> { [pkSVer] = pkS }; var clientSksDictionary = new Dictionary <int, SecretKey> { [skCVer] = skC }; if (updateTokens != null && updateTokens.Length > 0) { var updateTokenList = updateTokens.Select(UpdateToken.Decode) .Where(it => it.Version > skCVer) .OrderBy(it => it.Version) .ToList(); ctx.UpdateTokens = updateTokenList; foreach (var token in updateTokenList) { pkS = phe.RotatePublicKey(pkS, token.A, token.B); skC = phe.RotateSecretKey(skC, token.A, token.B); serverPksDictionary.Add(token.Version, pkS); clientSksDictionary.Add(token.Version, skC); } } ctx.clientSecretKeys = clientSksDictionary; ctx.serverPublicKeys = serverPksDictionary; return(ctx); }
public void Should_RotateTheSamePublicKey_When_OldPublicKeyAndUpdateTokenAreGiven() { var a = Bytes.FromString("T20buheJjFOg+rsxP5ADIS7G3htdY/MUt9VozMOgEfA=", StringEncoding.BASE64); var b = Bytes.FromString("UbXPXPtmKuudthZXXjJTE9AxBEgZB7mTFD+TGViCgHU=", StringEncoding.BASE64); var pkS = Bytes.FromString("BBqqpApF8EsvQtLQlcR1sBon9RbKDcrsNypYDGatbx5JxvdQfGaszDwen01xQVWxL0UvrLfmzTBJHpL+q5+kyWw=", StringEncoding.BASE64); var pkS1 = Bytes.FromString("BMiu/KcLEom9PwAeEeN9gYJZ45kdlYdo1bYPsd8YjWvRVgqJY2MzJlu2OR1d7ynxZvsdXbVY68pxG/oK3k+3xX0=", StringEncoding.BASE64); var phe = new PheCrypto(); var phePkC = phe.DecodePublicKey(pkS); var phePkC1 = phe.RotatePublicKey(phePkC, a, b); Assert.Equal(pkS1, phePkC1.Encode()); }