Java's mad5 is encrypted with a 32-bit string, and C # may not be 32-bit after direct encryption, and the number of bits is indeterminate.
Common wording.
public static string Md5 (String Sourcein)
{
var md5csp = new MD5CryptoServiceProvider ();
byte[] Md5source = Encoding.UTF8.GetBytes (Sourcein);
byte[] Md5out = Md5csp.computehash (Md5source);
Return convert.tobase64string (md5out);
}
The result must be different from Java.
If you want to implement compatibility: C # 's notation changes:
using UnityEngine;
using System.IO;
/// <summary>
/// camera
/// <para> ZhangYu 2018-07-06 </ para>
/// </ summary>
public class CameraCapture: MonoBehaviour {
// size
public enum CaptureSize {
CameraSize,
ScreenResolution,
FixedSize
}
// target camera
public Camera targetCamera;
// size
public CaptureSize captureSize = CaptureSize.CameraSize;
// pixel size
public Vector2 pixelSize;
// save route
public string savePath = "StreamingAssets /";
// file name
public string fileName = "cameraCapture.png";
#if UNITY_EDITOR
private void Reset () {
targetCamera = GetComponent <Camera> ();
pixelSize = new Vector2 (Screen.currentResolution.width, Screen.currentResolution.height);
}
#endif
/// <summary> save </ summary>
/// <param name = "camera"> target camera </ param>
public void saveCapture () {
Vector2 size = pixelSize;
if (captureSize == CaptureSize.CameraSize) {
size = new Vector2 (targetCamera.pixelWidth, targetCamera.pixelHeight);
} else if (captureSize == CaptureSize.ScreenResolution) {
size = new Vector2 (Screen.currentResolution.width, Screen.currentResolution.height);
}
string path = Application.dataPath + "/" + savePath + fileName;
saveTexture (path, capture (targetCamera, (int) size.x, (int) size.y));
}
/// <summary> camera </ summary>
/// <param name = "camera"> target camera </ param>
public static Texture2D capture (Camera camera) {
return capture (camera, Screen.width, Screen.height);
}
/// <summary> camera </ summary>
/// <param name = "camera"> target camera </ param>
/// <param name = "width"> width </ param>
/// <param name = "height"> Height </ param>
public static Texture2D capture (Camera camera, int width, int height) {
RenderTexture rt = new RenderTexture (width, height, 0);
rt.depth = 24;
rt.antiAliasing = 8;
camera.targetTexture = rt;
camera.RenderDontRestore ();
RenderTexture.active = rt;
Texture2D texture = new Texture2D (width, height, TextureFormat.ARGB32, false, true);
Rect rect = new Rect (0, 0, width, height);
texture.ReadPixels (rect, 0, 0);
texture.filterMode = FilterMode.Point;
texture.Apply ();
camera.targetTexture = null;
RenderTexture.active = null;
Destroy (rt);
return texture;
}
/// <summary> save stickers </ summary>
/// <param name = "path"> save path </ param>
/// <param name = "texture"> Texture2D </ param>
public static void saveTexture (string path, Texture2D texture) {
File.WriteAllBytes (path, texture.EncodeToPNG ());
#if UNITY_EDITOR
Debug.Log ("Saved to:" + path);
#endif
}
}
Attention
using UnityEditor;
using UnityEngine;
/// <summary>
/// Camera editor
/// <para> ZhangYu 2018-07-06 </ para>
/// </ summary>
[CanEditMultipleObjects]
[CustomEditor (typeof (CameraCapture))]
public class CameraCaptureEditor: Editor {
public override void OnInspectorGUI () {
// Attributes
CameraCapture script = (CameraCapture) target;
int selected = (int) script.captureSize;
// Redraw the GUI
EditorGUI.BeginChangeCheck ();
drawProperty ("targetCamera", "Target Camera");
string [] options = new string [] {"Camera size", "Screen size", "Fixed size"};
selected = EditorGUILayout.Popup ("Size", selected, options, GUILayout.ExpandWidth (true));
script.captureSize = (CameraCapture.CaptureSize) selected;
if (script.captureSize == CameraCapture.CaptureSize.FixedSize) {
drawProperty ("pixelSize", "Pixel Size");
EditorGUILayout.HelpBox ("Please keep the correct aspect ratio! \ NOtherwise there may be errors in the area.", MessageType.Info);
}
drawProperty ("savePath", "Save Path");
drawProperty ("fileName", "file name");
// save button
bool isPress = GUILayout.Button ("Save", GUILayout.ExpandWidth (true));
if (isPress) script.saveCapture ();
if (EditorGUI.EndChangeCheck ()) serializedObject.ApplyModifiedProperties ();
}
private void drawProperty (string property, string label) {
EditorGUILayout.PropertyField (serializedObject.FindProperty (property), new GUIContent (label), true);
}
}
ToString ("X2") is a string format control character in C #
X is hexadecimal
2 is a double digit every time.
such as 0x0A, if not 2, will only output 0xA
Assuming that there are two numbers 10 and 26, the normal hexadecimal display 0xA, 0x1A, so it looks untidy, in order to look good, you can specify "X2", so that the display is: 0x0A, 0x1A.
Java and C#MD5 encryption are different